Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIStatusBarStyle not working in Swift

I'm trying to change the Status Bar color in my Swift app to white, but am hitting a brick wall. I have 3 ViewControllers that are each embedded in a NavigationController (could that be the issue? I've already tried to place the code in the NavigationController class.) I've tried both of the following pieces of code in the didFinishLaunchingWithOptions of my AppDelegate.swift file but neither worked.

application.statusBarStyle = .LightContent 

and

UIApplication.sharedApplication().statusBarStyle = .LightContent 

All that the Docs have to say about it is that UIBarButtonStyle is an Int and gave me this enum snippet which didn't help me at all with implimentation.

enum UIStatusBarStyle : Int {     case Default     case LightContent     case BlackOpaque } 

What am I missing?

like image 637
davidrayowens Avatar asked Jun 16 '14 00:06

davidrayowens


People also ask

How do I contact preferredStatusBarStyle?

On a UINavigationController, preferredStatusBarStyle is not called because its topViewController is preferred to self . So, to get preferredStatusBarStyle called on an UINavigationController, you need to change its childForStatusBarStyle (Swift) / childViewControllerForStatusBarStyle (ObjC).

How do I change the color of my status bar in Swiftui?

You can change the status bar's color and material by inserting a small view right behind it. Normally, all views are positioned below the status bar, but you can use edgesIgnoringSafeArea(. top) to push it past the safe area insets.


Video Answer


1 Answers

You have two options.

If you want to continue manually setting the style of the status bar, continue doing what you're doing, but you'll need to add the following key to your info.plist file with a value of NO.

View controller-based status bar appearance

Or, if you want to continue to use view controller based status bar appearance, instead of setting the application's statusBarStyle, override the preferredStatusBarStyle property in each view controller for which you'd like to specify a status bar style.

Swift 3

override var preferredStatusBarStyle: UIStatusBarStyle {     return .lightContent } 

Swift 2

override func preferredStatusBarStyle() -> UIStatusBarStyle {     return UIStatusBarStyle.LightContent } 
like image 51
Mick MacCallum Avatar answered Sep 20 '22 00:09

Mick MacCallum