Preferred status bar style of view controller is ignored when in navigation controller

微笑、不失礼 提交于 2019-12-17 23:17:54

问题


I'm writing an iOS App with multiple views. I've set the App to use ViewController-based status bar style, which allows me to use the following code

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
} 

That worked like expected.

But then I've embedded the views in a navigation controller and connected a BarButtonItem with a showSegue. Since then the ViewController of the view switched to ignores the style settings and shows the default black status bar.


回答1:


When you're in a navigation controller that will not get called. The navigation controller's preferredStatusBarStyle will be called. Try this along with your code:

extension UINavigationController {

   open override var preferredStatusBarStyle: UIStatusBarStyle {
      return topViewController?.preferredStatusBarStyle ?? .default
   }
}



回答2:


There is a solution that is a bit more concise (and recommended by Apple):

extension UINavigationController {
    override open var childForStatusBarStyle: UIViewController? {
        return topViewController
    }
}


来源:https://stackoverflow.com/questions/41026514/preferred-status-bar-style-of-view-controller-is-ignored-when-in-navigation-cont

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!