JayVDiyk JayVDiyk - 2 months ago 35
iOS Question

Set StatusBarStyle in Swift 3

I have set the

ViewController-Based Status Bar Appearance
to
true
in
info.plist
file.

in my ViewController, I set

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent;
}


But my status bar is still black. Any ideas?

KAR KAR
Answer

First Step:

Open your info.plist and insert a new key named "View controller-based status bar appearance" to NO

Second Step (Just an explanation, no need to implement this):

Normally we put the following code in the application(_:didFinishLaunchingWithOptions:) method of the AppDelegate,

UIApplication.sharedApplication().statusBarStyle = .LightContent

but that affects the StatusBarStyle of all the ViewControllers.

So, how to get this working for specific ViewControllers - Final Step:

Open the viewcontroller file where you want to change the StatusBarStyle and put the following code in viewDidLoad(),

UIApplication.sharedApplication().statusBarStyle = .LightContent

Also, implement the viewWillDisappear() method for that specific viewController and put the following lines of code,

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

}

This step will first change the statusBarStyle for the specific viewcontroller and then change it back to default when the specific viewcontroller disappears. Not implementing the viewWillDisappear() will change the StatusBarStyle permanently to the new defined value of UIStatusBarStyle.LightContent

Comments