11

I want to apply Light Content style to the whole application.

Following method is deprecated in iOS 9 without the replacement method.

-setStatusBarStyle:animated:

Sets the style of the status bar, optionally animating the transition to the new style.

Is working in the AppDelegate like as :

Swift 1.2 Code :

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

But when I upgrade my project to iOS 9/Swift 2 they are giving me warning message about :

<Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextTranslateCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
<Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

So as per the solution from the apple forum and from this answer CGContextSaveGState: invalid context 0x0 Error only on device

So, I have removed the property UIViewControllerBasedStatusBarAppearance from the info.plist file as per the solution.

Now the situation is that How we can set the Light Content style of Status Bar in iOS 9 for whole application?

From the forum :

enter image description here

I don't want to set the Light Content for each and every view.

Any suggestions?

Objective-C should have the same issue.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136

2 Answers2

28

You can save yourself a lot of work by using a UINavigationController and setting its navigation bar's barStyle to .Black. You only have to do this once; you can do it in the storyboard editor.

This will automatically turn your status bar's style to .LightContent as long as this UINavigationController is in command.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks for the answer, I will check and let you know. – Ashish Kakkad Sep 23 '15 at 03:12
  • Woooa! This is working fine :) Thanks a lot. Can you suggest what we have to do if there is no `UINavigationController`? – Ashish Kakkad Sep 23 '15 at 03:16
  • 2
    In that case you would need to put your entire interface into a custom parent container view controller that serves as the root view controller of the whole app, and implements `preferredStatusBarStyle`. Either that or just bite the bullet and implement `preferredStatusBarStyle` everywhere. – matt Sep 23 '15 at 03:19
  • 2
    Ok. Thank you. Please update the answer with this statement. So anyone can get help from your answer. – Ashish Kakkad Sep 23 '15 at 03:20
  • 2
    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlack]; – elprup Oct 25 '15 at 15:42
  • 1
    For those of you who are using a splitView controller, make sure you set the preferredBarStyle on that ... – ArdenDev May 27 '16 at 13:20
0

You can also used: UIApplication.shared.statusBarStyle = .lightContent. It also works in swift3.

Amanpreet
  • 1,301
  • 3
  • 12
  • 29