尝试从模态视图中隐藏状态栏。
已经检查了几种方法:
override func prefersStatusBarHidden() -> Bool {
return true
}
with / without self.setNeedsStatusBarAppearanceUpdate()
也
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: .Fade)
但在iOS 9中折旧
这适用于全屏演示(模态segue演示选项),但请注意全屏幕,这是我的设置。
如果您有任何想法..
答案 0 :(得分:44)
对于View Controller的非全屏演示文稿,您需要使用undefined
属性。
e.g。
modalPresentationCapturesStatusBarAppearance
对于View Controller的全屏演示文稿,您需要:
toViewController.modalTransitionStyle = .coverVertical
toViewController.modalPresentationStyle = .overFullScreen
toViewController.modalPresentationCapturesStatusBarAppearance = true
fromViewController.present(toViewController,
animated: true,
completion: nil)
。modalPresentationStyle
prefersStatusBarHidden
值设置为YES e.g。
UIViewControllerBasedStatusBarAppearance
(是的,iOS中的状态栏设置非常糟糕。难怪Stack Overflow在这个问题上有很多问题,答案也很多。)
答案 1 :(得分:1)
确实FullScreen
状态栏更新会自动调用,但不会调用OverFullScreen
。
此外,在我的情况下,我需要处理堆栈中的导航控制器,以传递 ModalViewController 作为孩子:
extension UINavigationController {
public override func childViewControllerForStatusBarHidden() -> UIViewController? {
return self.visibleViewController
}
public override func childViewControllerForStatusBarStyle() -> UIViewController? {
return self.visibleViewController
}
}
在 ModalViewController 内部,我们手动更新状态栏,为了使其顺利进行,我们必须在viewWillDisappear
中执行此操作,但此时visibleViewController
仍然 ModalViewController ,没有任何内容可以使用内部bool statusBarHidden并相应地更新它
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.statusBarHidden = true
self.setNeedsStatusBarAppearanceUpdate()
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.statusBarHidden = false
self.setNeedsStatusBarAppearanceUpdate()
}
override func prefersStatusBarHidden() -> Bool {
return self.statusBarHidden
}
答案 2 :(得分:1)
要在执行全屏模式时隐藏状态栏,您需要在viewDidLoad中设置它:
override func viewDidLoad() {
super.viewDidLoad()
modalPresentationCapturesStatusBarAppearance = true
}
然后执行隐藏状态栏的标准方法:
override var prefersStatusBarHidden: Bool {
return true
}
答案 3 :(得分:0)
如果您正在使用故事板并且想要隐藏/显示状态栏,则可以在以前的视图控制器上使用此方法:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
UIApplication.shared.setStatusBarHidden(false, with: UIStatusBarAnimation.none)
}