UIStatusBarStyle not working in Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIStatusBarStyle controls whether the status bar text and icons appear light or dark. The most common reason it stops working is that UIViewControllerBasedStatusBarAppearance is set to NO in Info.plist while trying to set the style per view controller, or the view controller hierarchy (especially UINavigationController) overrides individual controller preferences. In iOS 13+ with dark mode, the .default style adapts automatically, and UINavigationBarAppearance takes over status bar styling in navigation-based apps. Fixing this requires understanding which object controls the status bar in your specific view controller hierarchy.
Setting Status Bar Style Per View Controller
preferredStatusBarStyle is a computed property that the system reads when it needs the status bar style. Call setNeedsStatusBarAppearanceUpdate() to tell the system to re-query this property after a change.
The Info.plist Setting
If UIViewControllerBasedStatusBarAppearance is NO, the system ignores preferredStatusBarStyle on all view controllers. The default value is YES (since iOS 7), so if you never set it, per-controller styling works. But some third-party libraries or templates set it to NO, which breaks per-controller control.
UINavigationController Overrides
UINavigationController, UITabBarController, and UISplitViewController determine the status bar style for their children. Override childForStatusBarStyle to delegate the decision to a specific child view controller.
UINavigationBarAppearance (iOS 13+)
Setting barStyle = .black on a navigation bar forces the status bar to use light content, regardless of preferredStatusBarStyle.
SwiftUI Status Bar Style
In SwiftUI, .preferredColorScheme(.dark) sets the entire view to dark mode appearance, which includes a light status bar. Use .toolbarColorScheme (iOS 16+) for more granular control in navigation stacks.
Debugging Status Bar Style
Walk the view controller hierarchy to find which controller is actually determining the status bar style. The topmost container controller wins unless it delegates via childForStatusBarStyle.
Modal Presentation
Only .fullScreen modal presentations automatically transfer status bar control to the presented controller. For other styles, set modalPresentationCapturesStatusBarAppearance = true.
Common Pitfalls
- UIViewControllerBasedStatusBarAppearance set to NO: This disables per-view-controller styling entirely. Remove this key from Info.plist or set it to
YES. The legacyUIApplication.shared.statusBarStylesetter was deprecated in iOS 9. - UINavigationController swallowing the style: Navigation controllers do not forward
preferredStatusBarStyleto their children by default. SubclassUINavigationControllerand overridechildForStatusBarStyleto returntopViewController. - Forgetting setNeedsStatusBarAppearanceUpdate(): Changing a property that affects
preferredStatusBarStyledoes not automatically update the status bar. CallsetNeedsStatusBarAppearanceUpdate()after the change to trigger a refresh. - barStyle conflicting with preferredStatusBarStyle: Setting
navigationBar.barStyle = .blackforces light status bar content regardless of the view controller'spreferredStatusBarStyle. Remove thebarStyleoverride or set it to.defaultto allow per-controller styling. - Dark mode automatic adaptation: In iOS 13+,
.defaultadapts to the current user interface style (dark text in light mode, light text in dark mode). If you hardcode.lightContentand the user switches to light mode, the status bar may become invisible against a white background.
Summary
- Override
preferredStatusBarStylein your view controller and callsetNeedsStatusBarAppearanceUpdate()to trigger it - Ensure
UIViewControllerBasedStatusBarAppearanceisYES(or absent) in Info.plist - Subclass
UINavigationControllerto forwardchildForStatusBarStyletotopViewController - Use
navigationBar.barStyle = .blackto force light status bar content in navigation stacks - In SwiftUI, use
.preferredColorScheme(.dark)or.toolbarColorSchemefor status bar control - For modals that are not full screen, set
modalPresentationCapturesStatusBarAppearance = true
Related reading
- UITableView - change section header color
- UITableView - scroll to the top
- UITableView Add content offset at top
- UITableView Cell selected Color?
- UITableView not showing first time, only when scrolling a bit
- UITableViewCell show white background and cannot be modified on iOS7
- UITableView clear background
- UITableView disable swipe to delete, but still have delete in Edit mode?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.