Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I properly change my status bar style in swift 2/ iOS 9?

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

like image 826
John Jackson Avatar asked Sep 19 '15 23:09

John Jackson


People also ask

Can we change status bar color in IOS?

Select the View and in the Attributes Inspector change the Background Color to Light Gray. Build and Run the Project. The default style of the status bar is dark content. The style of the status bar can be changed to a status bar with white content.

How do you make a status bar light in Swift?

Open you Info. Add View controller-based status bar appearance key ( UIViewControllerBasedStatusBarAppearance ) and set value to No ( false ). Add Status bar style key ( UIStatusBarStyle ) and set value to Light Content ( UIStatusBarStyleLightContent ).


3 Answers

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

like image 188
Tom Hughes Avatar answered Oct 12 '22 17:10

Tom Hughes


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.

like image 45
dede.exe Avatar answered Oct 12 '22 17:10

dede.exe


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
like image 8
MRustamzade Avatar answered Oct 12 '22 17:10

MRustamzade