Jacob Jidell
Jacob Jidell

Reputation: 2792

Setting statusbarStyle (deprecated in iOS 9.0)

Just downloaded the new xCode 10.0 and saw that the old statusBarStyle has been deprecated since iOS 9.0.

Warning: Setter for 'statusBarStyle' was deprecated in iOS 9.0: Use -[UIViewController preferredStatusBarStyle]

Deprecated code: UIApplication.shared.statusBarStyle = .default

I tried using self.preferredStatusBarStyle, but found out the property is only a getter. So anyone knows how to set the statusBarStyle?


Edit

I want to change the statusBarStyle inside a function, where a user can switch between different themes. For example:

func changeStatusBar(toDarkMode: Bool) {
    if toDarkMode {
        // Set to light statusBarStyle
    } else { 
        // Set to default
    }
}

Upvotes: 44

Views: 32853

Answers (6)

hesham ghalaab
hesham ghalaab

Reputation: 31

My solution was as this: making an extension from the navigation controller:

extension UINavigationController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        if let topViewController = presentedViewController{
            return topViewController.preferredStatusBarStyle
        }
        if let topViewController = viewControllers.last {
            return topViewController.preferredStatusBarStyle
        }

        return .default
    }
}

and if you have a viewController that will have another style than the style of the app , you can make this

var barStyle = UIStatusBarStyle.lightContent
override var preferredStatusBarStyle: UIStatusBarStyle{
    return barStyle
}

lets say that you app status style is .default and you want this screen to be .lightContent so barStyle will take the .lightContent as its default value, this will change the status bar style to lightContent, and then make sure when viewWillDisappear change the barStyle again to the app status bar style which in our case is .default .

this is works for me

Upvotes: 2

cmilr
cmilr

Reputation: 379

None of the other suggestions worked for me. I ended up getting it to work by:

  1. Setting:

    override var preferredStatusBarStyle : UIStatusBarStyle {
        return .lightContent
    }
    
  2. Calling:

    setNeedsStatusBarAppearanceUpdate()
    

Upvotes: 5

Pavlos
Pavlos

Reputation: 934

Add View controller-based status bar appearance NO in Info.plist

And select Light in Status Bar Style in Deployment Info

enter image description here

Upvotes: 46

CryingHippo
CryingHippo

Reputation: 5086

If you use UINavigationController you also may want to use following code :

extension UINavigationController {
   open override var preferredStatusBarStyle: UIStatusBarStyle {
      return topViewController?.preferredStatusBarStyle ?? .default
   }
}

Reason is setNeedsStatusBarAppearanceUpdate() doesn't call childs preferredStatusBarStyle

Upvotes: 13

Paul King
Paul King

Reputation: 2001

Set your darkMode variable using the same code you have now, then use it in the computed variable that the system is expecting:

var darkMode = false
override var preferredStatusBarStyle : UIStatusBarStyle {
    return darkMode ? .default : .lightContent
}

Depending on the context you may need to force a refresh of the screen for it to take effect. You would do that with the following call:

setNeedsStatusBarAppearanceUpdate()

Upvotes: 35

Rashed
Rashed

Reputation: 2425

In swift4, You can use this block of code below viewDidLoad() in your ViewController-

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

Upvotes: 27

Related Questions