Changing the Status Bar Color for specific ViewControllers using Swift in iOS8 Changing the Status Bar Color for specific ViewControllers using Swift in iOS8 swift swift

Changing the Status Bar Color for specific ViewControllers using Swift in iOS8


After reading all the suggestions, and trying out a few things, I could get this to work for specific viewcontrollers using the following steps :

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,

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

UIApplication.shared.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 viewWillAppear(),

Swift 2

UIApplication.sharedApplication().statusBarStyle = .LightContent

Swift 3

UIApplication.shared.statusBarStyle = .lightContent

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

Swift 2

override func viewWillDisappear(animated: Bool) {    super.viewWillDisappear(animated)    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default}

Swift 3

override func viewWillDisappear(_ animated: Bool) {    super.viewWillDisappear(animated)    UIApplication.shared.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


(As of August 25, 2020)

Swift 5, Swift 4.2, Swift 4

override func viewWillAppear(_ animated: Bool) {    super.viewWillAppear(animated)    setNeedsStatusBarAppearanceUpdate()}override var preferredStatusBarStyle: UIStatusBarStyle {    .lightContent}


Swift 4.2 solution with NavigationController

First Step:

Open your info.plist and insert a new key named "View controller-based status bar appearance" or UIViewControllerBasedStatusBarAppearance to YES to let each VC use their own status property.

Second Step

In each VC, override the preferredStatusBarStyle property like this :

override var preferredStatusBarStyle : UIStatusBarStyle {    return .lightContent //.default for black style}

Last step

Override the preferredStatusBarStyle property in your custom NavigationController class :

class NavigationController : UINavigationController {override var preferredStatusBarStyle : UIStatusBarStyle {    if let topVC = viewControllers.last {        //return the status property of each VC, look at step 2        return topVC.preferredStatusBarStyle      }    return .default}