Fix: preferredStatusBarStyle Not Called in UIKit View Controllers

Fix: preferredStatusBarStyle Not Called in UIKit View Controllers

Written by

in

Quick answer: The preferredStatusBarStyle method isn’t called when the view controller isn’t the window’s root view controller, or when it’s embedded in a UINavigationController that manages status bar style via its navigationBar.barStyle property. Set the root view controller in AppDelegate, or set navigationBar.barStyle to UIBarStyleBlack for light content.

Why this happens

The preferredStatusBarStyle method on UIViewController is part of the view controller-based status bar appearance system introduced in iOS 7. For this system to work, the view controller implementing the method must be part of the view controller hierarchy that UIKit queries for status bar appearance.

Two common scenarios prevent the method from being called:

  1. Missing root view controller assignment — If your UIWindow doesn’t have its rootViewController property set in application(_:didFinishLaunchingWithOptions:), UIKit has no view controller hierarchy to query. Any preferredStatusBarStyle implementation in your view controllers will be ignored because they’re not connected to the window.

  2. Embedding in UINavigationController — A UINavigationController does not forward preferredStatusBarStyle calls to its child view controllers. The navigation controller manages status bar appearance itself, basing it on its navigationBar.barStyle property. Child view controllers’ implementations are never consulted.

How to fix it

1. Set the root view controller in AppDelegate (most common fix)

Ensure your window’s rootViewController is assigned before calling makeKeyAndVisible(). This connects your view controller hierarchy to the window so UIKit can query status bar appearance.

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    let rootTabBarController = UITabBarController()
    // Configure your tab bar controller with view controllers
    window?.rootViewController = rootTabBarController
    window?.makeKeyAndVisible()
    return true
}

If you’re using a storyboard, the initial view controller is set automatically. For programmatic setup, this assignment is mandatory.

After setting the root view controller, any view controller in the hierarchy can control status bar style by overriding preferredStatusBarStyle and calling setNeedsStatusBarAppearanceUpdate() when the style should change:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

// Call when background color changes
setNeedsStatusBarAppearanceUpdate()

2. Configure UINavigationController barStyle for embedded view controllers

When your view controller is inside a UINavigationController, don’t override preferredStatusBarStyle in the child. Instead, set the navigation bar’s barStyle on the navigation controller:

// In your view controller or where you configure the navigation controller
navigationController?.navigationBar.barStyle = .black // Yields light status bar content
// or
navigationController?.navigationBar.barStyle = .default // Yields dark status bar content

UIBarStyle.black produces UIStatusBarStyle.lightContent (white text), while UIBarStyle.default produces UIStatusBarStyle.default (dark text).

If you need per-view-controller control within a navigation stack, subclass UINavigationController and override preferredStatusBarStyle to forward to the top view controller:

class CustomNavigationController: UINavigationController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return topViewController?.preferredStatusBarStyle ?? .default
    }
}

Then use your custom navigation controller class throughout the app.

3. Legacy: Opt out of view controller-based appearance (not recommended)

As a last resort, you can disable view controller-based status bar appearance entirely by setting UIViewControllerBasedStatusBarAppearance to NO in Info.plist. Then use UIApplication.shared.setStatusBarStyle(_:) directly. This approach is deprecated since iOS 9 and should be avoided in modern apps.

How to prevent it

  • Always assign window.rootViewController in application(_:didFinishLaunchingWithOptions:) for programmatic window setup.
  • When using UINavigationController, control status bar style via navigationBar.barStyle or a custom navigation controller subclass.
  • Call setNeedsStatusBarAppearanceUpdate() after changing any state that affects preferredStatusBarStyle.
  • Test status bar appearance on both light and dark backgrounds during development.
  • Prefer the view controller-based system (UIViewControllerBasedStatusBarAppearance = YES, the default) over the deprecated UIApplication API.

FAQ

Why does preferredStatusBarStyle work on one screen but not another?

The view controller must be in the window’s root view controller hierarchy. If you present a view controller modally without a navigation controller, it becomes the top-most controller and its preferredStatusBarStyle is queried. But if it’s pushed onto a navigation stack, the navigation controller takes over.

Can I use preferredStatusBarStyle with SwiftUI?

SwiftUI doesn’t use preferredStatusBarStyle directly. For SwiftUI views in a UIHostingController, override preferredStatusBarStyle on the hosting controller subclass. For mixed UIKit/SwiftUI apps, ensure the hosting controller is in the window hierarchy.

Does this apply to iPad multitasking or split view?

Yes. In split view or slide over, each window scene has its own root view controller hierarchy. The same rules apply: the view controller must be connected to its scene’s window for preferredStatusBarStyle to be called.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.