48
votes

I'm attempting to change my status bar's style to .Light but the previous code I implemented in swift 1.2 seems not to work anymore.. here's the code:

override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.sharedApplication().statusBarStyle = .LightContent

    }

now I have my View controller-based status bar appearance info.plist setting to YES, and reading the UIKit doc, this will negate any statusBarStyle changes and keep it at default. However when I change the setting to 'NO' and change the statusBarStyle, I get this <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable in my debugger.. So is this a bug in Xcode? because to change the status bar style you must change info.plist setting to NO, but when that happens.. error

12

12 Answers

72
votes

Apple have added the capability to change the status bar style in the deployment info. Simply choose 'Light'.ScreenShot of Xcode

Also set View controller-based status bar appearance key to NO in the Info.plist

info plist

45
votes

I always did this way.

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    //Changing Status Bar
    override func preferredStatusBarStyle() -> UIStatusBarStyle {

        //LightContent
        return UIStatusBarStyle.LightContent

        //Default
        //return UIStatusBarStyle.Default
    }
}

It works in any swift 2.x version. This requires that you set View controller-based status bar appearance in your Info.plist file to YES.

8
votes

Swift 3 just add View controller-based status bar appearance with value NO to info.plistand then add to ViewControllerwhere you want:

UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
7
votes

You can still use preferredStatusBarStyle in your view controller:

step 1: in the info.plist set ViewControllerBasedStatusBarAppearance to YES. step 2: add this code to the ViewController you'd like to edit :

override func  preferredStatusBarStyle() -> UIStatusBarStyle {

    return UIStatusBarStyle.LightContent
}

*** Tip: It seems to only work outside of the ViewDidLoad(), didReceiveMemoryWarning() functions.

6
votes

The change in deployment info works but despite - you need to add the 'View controller-based status bar appearance' key to plist file setting it to NO.

5
votes

You can also just add this in the AppDelegate. This option is better if you want to change it for every ViewController in the app and not have to make it different for every other VC.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    application.statusBarStyle = UIStatusBarStyle.LightContent
    // instead of 
    // UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false)
    // which gives warning about deprecation in iOS 9

    return true
}
4
votes

It looks like it's a bug in Xcode 7.0. I'm also getting the Error>: CGContextSaveGState: invalid context 0x0. error when setting View controller-based status bar appearance

For now I'm just overriding the status bar color in every view controller.

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return .LightContent
} 
4
votes

You can choose "light" in the deployment info, but then you also need to add the "View controller-based status bar appearance" and set it to NO.

4
votes

Here try this it might help you

First goto info.plist file and add this "View controller-based status bar appearance" as a key and set the value as NO

here below shown in the imageenter image description here

after this come to AppDelegate.swift file and past this UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent line of code in

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool{
 UIApplication.sharedApplication().statusBarStyle =   UIStatusBarStyle.LightContent
 return true
}

like this

4
votes

For swift 3 override the preferredStatusBarStyle variable use this:

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

The existing answers are great, but it's a little different now with the new updates!

override var now instead of override func for anyone confused - the gist is still the same and you still need to change your 'Info.plist':

override var preferredStatusBarStyle: UIStatusBarStyle 
{    
    //LightContent
    return UIStatusBarStyle.lightContent

    //Default
    //return UIStatusBarStyle.default
}
0
votes

If you want to change it from time to time inside your app, you can use the overrides preferredStatusBarStyle() as mentioned before.

Just make sure, that you also call setNeedsStatusBarAppearanceUpdate() after calling preferredStatusBarStyle(), to inform IOS about it.