I like the smell of Swift in the morning…

Setting the Status Bar Style without pulling your hair out

Posted: | Author: | Filed under: iOS, Swift | Tags: | No Comments »

Some things are inexplicably hard in iOS. Like changing the color of your app’s status bar. In theory it is really easy:

  • Set UIViewControllerBasedStatusBarAppearance to true in your Info.plist
  • Override preferredStatusBarStyle in your view controllers to return your desired UIStatusBarStyle

So why doesn’t it work in some cases? Chances are that you are trying to set the status bar style in a view controller that is embedded in a UINavigationController. In that case the system is asking the UINavigationController for its preferredStatusBarStyle. The override in your custom view controller is simply ignored.

To fix this you can use this handy little extension on UINavigationController:

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

Now the UINavigationController gets the preferredStatusBarStyle from its topViewController (your custom view controller). Or it does return the default value if there is no topViewController (which should never happen)

Leave a Reply