Search code examples
iosswiftstatusbar

Change Status Bar style in XCode 8.2 / Swift 3.0 (No "View controller-based status bar appearance")


I am trying to modify the appearance of my status bar (make text white/ set the Style to "light"). I managed to set the background color by adding this to my AppDelegate.swift file:

let statWindow = UIApplication.shared.value(forKey:"statusBarWindow") as! UIView
let statusBar = statWindow.subviews[0] as UIView
statusBar.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)

However, when I go to change the style of the text of the status bar, even changing this under General > Deployment Info > Status Bar Style (changing this to "Light") does not work.

I also tried to modify the status bar through Info.plist, but there is no field for "View controller-based status bar appearance" (see second image). Also, there is no option for a "light" style under the Status bar style option (see below image):

Status bar style options: ScreenShot

No view controller status bar field: ScreenShot


Solution

  • In each UIViewController of your application, you should override preferredStatusBarStyle property:

    override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
    

    and eventually, call:

    <your controller>.setNeedsStatusBarAppearanceUpdate()
    

    If this statusBar style is throughout all your application, you should make a BaseViewController class that implement this, and make all you view controllers inherit from BaseViewController.