4

I'm finding it hard to change the status bar style programatically.

I see how to statically set it for each ViewController using a combo of (in ViewController.swift):

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.default
}

and (in info.plist):

View controller-based status bar appearance = YES

...

I'm looking to change it whenever I want!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Chris Allinson
  • 1,837
  • 2
  • 26
  • 39

3 Answers3

7

Found the answer after quite a lot of digging!

Set (in info.plist):

View controller-based status bar appearance = NO

and remove the (in ViewController.swift):

override var preferredStatusBarStyle: UIStatusBarStyle {
    return UIStatusBarStyle.default
}

...

Now you can use (in ViewController.swift):

UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)

And, to initially set the style for each ViewController, use viewDidAppear:

override func viewDidAppear(_ animated: Bool) {
    UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: false)
}
Chris Allinson
  • 1,837
  • 2
  • 26
  • 39
  • 3
    This seems to be deprecated in iOS 9 and it says to use UIViewController - preferredStatusBarStyle instead. But I can't get it to work. Did you find a solution to that? – henrik-dmg Feb 24 '17 at 11:10
  • ^ try setting (in Info.plist) "View controller-based status bar appearance = YES" and then in each viewcontroller override the preferredStatusBarStyle variable :) – Chris Allinson Feb 28 '17 at 02:37
  • @iOSwarrior It doen't work when you have a navigation bar in your view. Use "UIApplication.shared.statusBarStyle = .default" instead – Hanushka Suren Mar 01 '17 at 13:42
  • 1
    @ChrisAllinson what I meant was that this .setStatusBarStyle(UIStatusBarStyle.lightContent, animated: false) call is deprecated in iOS 9, so I can't figure out how to change the status bar style again after I override it, the initial setting is clear, but I want to dynamically change it, depending e.g. on screen brightness – henrik-dmg Mar 02 '17 at 12:36
7

swift 3

1.Change in info.plist the row View controller-based status bar appearance and set it to NO

2.Change in appDelegate.swift in didFinishLaunchingWithOptions

UIApplication.shared.statusBarStyle = .lightContent
Umesh Verma
  • 876
  • 7
  • 28
0

Store the status bar style as a property in your view controller:

var statusBarStyle: UIStatusBarStyle = .default

And then implement preferredStatusBarStyle in the same view controller:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return statusBarStyle
}

Then when you change statusBarStyle make sure to also call setNeedsStatusBarAppearanceUpdate. The preferredStatusBarStyle method is automatically called when the view appears/disappears, but if you change the status bar style while your view is visible, you have to tell the view controller the status bar appearance needs updating.

Note you still need to make the changes to the plist, and if your view controller is in a navigation controller, you may need to handle the status bar changes there instead (via a UINavigationController subclass, for example).

UIApplication.shared.setStatusBarStyle(…) was deprecated in iOS 9.0, so don't use that.

shim
  • 9,289
  • 12
  • 69
  • 108