prefersStatusBarHidden not called

2020-05-25 01:38发布

I have a UITabViewController -> UINavigationController -> UIViewController and want to hide and unhide the statusBar. when I call setNeedsStatusBarAppearanceUpdate() the method prefersStatusBarHidden is not called.

func fadeOutStatusBar (notification: NSNotification) {
    statusBarHidden = true
    self.setNeedsStatusBarAppearanceUpdate()
}

func fadeInStatusBar (notification: NSNotification) {
    statusBarHidden = false
    self.setNeedsStatusBarAppearanceUpdate()
}

override func prefersStatusBarHidden() -> Bool {
    return statusBarHidden
}

标签: ios statusbar
9条回答
姐就是有狂的资本
2楼-- · 2020-05-25 02:10

you can write an extension of UINavigationController that overrides its default implementation and returns the top ViewController.

  extension UINavigationController {
    override public func childViewControllerForStatusBarHidden() -> UIViewController{
       return self.topViewController
    }
   }
查看更多
家丑人穷心不美
3楼-- · 2020-05-25 02:12

Property prefersStatusBarHidden is being called on the root view controller of the current view controller.

This means that if your app is based on a UISplitViewController for example, you must implement the property in a custom UISplitViewController class.

查看更多
啃猪蹄的小仙女
4楼-- · 2020-05-25 02:13

For Swift 4.2 iOS 12

Assuming you have a ViewController contained within UINavigationController. Create your own Subclass of UINavigationController and include in it:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

Make sure info.plist sets View Controller based setting of status bar

查看更多
登录 后发表回答