问题描述
我有一些代码可以通过隐藏UINavigationController的导航栏进入全屏模式。进入全屏模式时,我希望获得平滑的动画缩放效果。我使用
在您的情况下,您同时使用 barTintColor
& navigationBarStyle
和 Show
Hide
动画。
barTintColor会覆盖样式属性所隐含的值
您应该选择 barTintColor
或 navigationBarStyle
在下面的代码中,我刚刚使用了 barTintColor
&默认值为navigationBarStyle,它带有 Transulent
。
var fullScreen = false {
didSet {
self.setNeedsStatusBarAppearanceUpdate()
}
}
覆写func viewDidLoad(){
super.viewDidLoad()
title = Navigation Bar
navigationController?.navigationBar.barTintColor = .red
}
覆盖func viewWillAppear(_动画:Bool){
super.viewWillAppear(true)
setFullScreen(on:fullScreen,animated:animation)
}
@IBAction func onToggleNavBarVisibility(_发送方:任意){
,如果让navBarHidden =
self.navigationController?.isNavigationBarHidden {
//切换状态
fullScreen =!navBarHidden
setFullScreen(on:fullScreen,animation:true)
}
}
private func setFullScreen(on:Bool,animated:Bool){
self.navigationController?.setNavigationBarHidden(在动画上:动画)
self.setNeedsStatusBarAppearanceUpdate()
}
编辑:
如果您想隐藏状态栏-
,请使用 prefersStatusBarHidden
和bool值。 &使用 setNeedsStatusBarAppearanceUpdate
覆盖var preferredsStatusBarHidden:Bool {
return全屏
}
I have code that enters full screen mode by hiding the UINavigationController's navigation bar. I want a smooth animated zooming effect when entering full screen. I use setNavigationBarHidden(_:animated:). This has all worked fine up to now, even on iOS 11, but on iPhone X the animation is not working well. On hiding, there is no animation and the nav bar just disappears. On unhiding, it does animate but the nav bar appears at a slower rate than the navigation controller's content area reduces, so an ugly black background shows through the navigation bar area during the animation.
I can recreate this in a simple test app. I have a UIViewController embedded in a UINavigationController.
Storyboard
- UINavigationController Navigation Bar: Style == Black; Translucent OFF
- UIViewController: Extend Edges: all options OFF.
I have tried all the combinations of Adjust Scroll View Insets and Extend Edges that I can think of but they made no difference.
Code
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setFullScreen(on: fullScreen, animated: animated)
}
override var prefersStatusBarHidden: Bool
{
return fullScreen
}
override var preferredStatusBarStyle: UIStatusBarStyle
{
return .lightContent
}
@IBAction func onToggleNavBarVisibility(_ sender: Any) {
if let navBarHidden = self.navigationController?.isNavigationBarHidden {
// Toggle the state
fullScreen = !navBarHidden
setFullScreen(on: fullScreen, animated: true)
}
}
private func setFullScreen(on : Bool, animated : Bool) {
self.navigationController?.setNavigationBarHidden(on, animated: animated)
self.setNeedsStatusBarAppearanceUpdate()
}
In your case you are using both barTintColor
& navigationBarStyle
with Show
Hide
animation.barTintColor overrides the value implied by the Style attributeYou should select either barTintColor
or navigationBarStyle
In below code i have just used barTintColor
& navigationBarStyle is default with Transulent
.
var fullScreen = false{
didSet{
self.setNeedsStatusBarAppearanceUpdate()
}
}
override func viewDidLoad() {
super.viewDidLoad()
title = "Navigation Bar"
navigationController?.navigationBar.barTintColor = .red
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
setFullScreen(on: fullScreen, animated: animated)
}
@IBAction func onToggleNavBarVisibility(_ sender: Any) {
if let navBarHidden =
self.navigationController?.isNavigationBarHidden {
// Toggle the state
fullScreen = !navBarHidden
setFullScreen(on: fullScreen, animated: true)
}
}
private func setFullScreen(on : Bool, animated : Bool) {
self.navigationController?.setNavigationBarHidden(on, animated: animated)
self.setNeedsStatusBarAppearanceUpdate()
}
EDIT:If you want to hide status bar-use prefersStatusBarHidden
with the bool value. & use setNeedsStatusBarAppearanceUpdate
override var prefersStatusBarHidden: Bool {
return fullScreen
}
https://developer.apple.com/documentation/uikit/uinavigationbar
这篇关于setNavigationBarHidden动画在iPhone X上无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!