Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change status bar style - iOS 12

Tags:

ios

swift

ios12

I need to update status bar style on every view controller based on the background color (what UINavigationController is doing automatically).

Have tried all the options described on stackoverflow (View controller-based status bar appearance in info.plist set to YES), but none worked for me.

I am using Xcode 10 beta 6 and Swift 4.2, targeting iOS 12.

like image 876
Tomáš Pánik Avatar asked Sep 07 '25 13:09

Tomáš Pánik


2 Answers

Swift 4+, iOS 12+

View controller-based status bar appearance now needs to be set to YES in info.plist since UIKit no longer wants us to edit status bar style through UIApplication.shared—status bar style is now view-controller based.

Then if you want the change to be applied at app level, simply override preferredStatusBarStyle in the appropriate container view controller (ideally the root)...

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

...and this will propagate to all view controllers underneath it. And if you want to edit status bar style per view controller, apply this override per view controller.

If status bar style ever changes during runtime, then you need to call setNeedsStatusBarAppearanceUpdate() (from anywhere in the container/root view controller or that specific view controller), otherwise it isn't needed.

like image 57
liquid LFG UKRAINE Avatar answered Sep 09 '25 09:09

liquid LFG UKRAINE


Set View controller-based status bar appearance to NO in the info.plist and override preferredStatusBarStyle in each view controller like so:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

And call setNeedsStatusBarAppearanceUpdate() in your view controller (in viewDidLoad() for example).

like image 29
ielyamani Avatar answered Sep 09 '25 10:09

ielyamani