简体   繁体   中英

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

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.

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
   }
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM