How to fix preferredStatusBarStyle not getting called

⋅ 2 min read ⋅ UIKit Status Bar

Table of Contents

There are two common reasons that make the preferredStatusBarStyle property not getting called.

  1. "View controller-based status bar appearance" key set to NO in Info.plist
  2. Your view controller is embedded in a UINavigationController or other Container View Controller.

View controller-based status bar appearance key set to No

If your project has the "View controller-based status bar appearance" key in the Info.plist, make sure it is set to "YES".

Having this flag set to "NO" tells the system to ignore whatever you set in preferredStatusBarStyle.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Embedded view controller

The preferredStatusBarStyle property won't get called if the view controller is embed in another container view controller, e.g., UINavigationController.

It doesn't get called because the one that gets called is its parent, UINavigationController.

This is because a parent view controller can have a custom view that covers the top part of the app, so it makes sense for the system to ask the parent view controller first.

A navigation view controller has a navigation bar that occupies the app's top part.
A navigation view controller has a navigation bar that occupies the app's top part.

If you try subclass UINavigationController and override preferredStatusBarStyle, you can verify that the preferredStatusBarStyle in a MyNavigationController is the one getting called.

class MyNavigationController: UINavigationController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}
The system call preferredStatusBarStyle of a navigation controller.
The system call preferredStatusBarStyle of a navigation controller.

If you want a navigation controller to give control of the status bar style to its children, override the childForStatusBarStyle property and return any child view controllers that you want to determines the status bar style.

In this case, we return visibleViewController, which represents either the view controller at the top of the navigation stack or a view controller that was presented modally on top of the navigation controller itself.

override var childForStatusBarStyle: UIViewController? {
return visibleViewController
}

With this change, the preferredStatusBarStyle of your child view controllers will get called again.


Read more article about UIKit, Status Bar, or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
How to change SwiftUI List section separator color

Learn how to colorize a list section separator.

Next
How to make parts of Text view Bold in SwiftUI

Learn how to style some parts of SwiftUI Text view.

← Home