18
votes

I've try to modify status bar color text but no one answer from this thread doesn't work. Any especially for XCode 6?

I've tried insert:

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}

to UIViewController

also

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

to AppDelegate.swift And I've tried change it in info.plist But it doesn't affect it. How change status bar color text to white?

5

5 Answers

51
votes

In your Info.plist you need to define View controller-based status bar appearance to any value.

enter image description here

If you define it YES then you should override preferredStatusBarStyle function in each view controller.

If you define it NO then you can set style in AppDelegate using

UIApplication.sharedApplication().statusBarStyle = .LightContent

3
votes

Just set " View controller-based status bar appearance == NO " in to your plist and put a single line in your appdelegate class in didfinshLaunching method.

 UIApplication.sharedApplication().statusBarStyle = .LightContent
2
votes

Swift 3.0

Just set View controller-based status bar appearance == NO in to your *.plist and put below code in your appdelegate class in didFinishLaunchingWithOptions method before return.

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
    statusBar.backgroundColor = UIColor.red
}
UIApplication.shared.statusBarStyle = .lightContent

You can change backgroundColor and statusBarStyle as per your requirement.

1
votes

Be sure to set the View controller-based status bar appearance in your info.plist file to Yes.

Furthermore, if you are in a UINavigationController, you cannot simply set the style in ViewControllers in it. Subclass the UINavigationController and add this to it:

override func preferredStatusBarStyle() -> UIStatusBarStyle {

    if let vc = self.viewControllers?.last as? UIViewController {
        return vc.preferredStatusBarStyle()
    }

    return super.preferredStatusBarStyle()
}

Now you can set the bar style in the UIViewController subclass and the UINavigationController will listen to it :).

0
votes

Keenle is right on, from iOS7 onward, you have to opt out of viewController-based status bar styles before you can set it app-wide.

doc:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/setStatusBarStyle:animated:

"To opt out of the view controller-based status bar appearance behavior, you must add the UIViewControllerBasedStatusBarAppearance key with a value of NO to your app’s Info.plist file, but doing so is not recommended."