how do I properly change my status bar style in sw

2019-01-16 12:04发布

问题:

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

回答1:

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

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



回答2:

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.



回答3:

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.



回答4:

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


回答5:

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.



回答6:

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
}


回答7:

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
} 


回答8:

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.



回答9:

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 image

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



回答10:

For swift 3 override the preferredStatusBarStyle variable use this:

 override var preferredStatusBarStyle: UIStatusBarStyle{
    return .lightContent
}


回答11:

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
}


回答12:

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.