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

Using the above code in any ViewController to set the statusBar color to White for a specific viewcontroller doesnt work in iOS8 for me. Any suggestions? Using the UIApplication.sharedApplication method, the color changes after required changes in the Info.plist for the whole app.

// Change the colour of status bar from black to white
UIApplication.sharedApplication().statusBarStyle = .LightContent

How can I just make changes to the status bar color for some required and specific ViewControllers?

1

37 Answers 37

1
2
1

For Xcode 10 you can create a class and put it before your viewController class, you can call this class in all view controller is needed a light content status bar...

class UIViewControllerWithLightStatusBar: UIViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
}

Now change your viewController class in:

class YourViewController: UIViewControllerWithLightStatusBar {
...
}

And that's all...

1

Works for Navigation Based for particular view controller in swift4

   let app = UIApplication.shared
   let statusBarHeight: CGFloat = app.statusBarFrame.size.height

   let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
   statusbarView.backgroundColor = UIColor.red
   view.addSubview(statusbarView)
1

WARNING


Setter for 'statusBarStyle' was deprecated in iOS 9.0: Use -[UIViewController preferredStatusBarStyle]

UIApplication.shared.statusBarStyle = .default

so my solution was as this: making an extension from the navigation controller:

extension UINavigationController {
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        if let topViewController = presentedViewController{
            return topViewController.preferredStatusBarStyle
        }
        if let topViewController = viewControllers.last {
            return topViewController.preferredStatusBarStyle
        }

        return .default
    }
}

and if you have a viewController that will have another style than the style of the app , you can make this

var barStyle = UIStatusBarStyle.lightContent
override var preferredStatusBarStyle: UIStatusBarStyle{
    return barStyle
}

lets say that you app status style is .default and you want this screen to be .lightContent so barStyle will take the .lightContent as its default value, this will change the status bar style to lightContent, and then make sure when viewWillDisappear change the barStyle again to the app status bar style which in our case is .default .

this is works for me

0

As of Oct 2020, Swift 5, Xcode 12

If you want to set it to all view controllers in the app. and if your app has a navigation controller.

You can do it in the plist file as follow: plist file changes

-2

Swift 3

In your AppDelegate file inside func application method

let statusBar: UIView = application.value(forKey: "statusBar") as! UIView
statusBar.backgroundColor = .red
-3

Swift 4.0 Please use this code in "didFinishLaunchingWithOptions launchOptions:" Appdelegate class

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

iOS 13

 var statusBarView: UIView = UIView()
        if #available(iOS 13.0, *) {
            let tag:UInt64 = 38482458385
            if let statusBar = UIApplication.shared.keyWindow?.viewWithTag(Int(tag)) {
                statusBar.backgroundColor = UIColor.red
                statusBarView = statusBar
            } else {
                let statusBar = UIView(frame: UIApplication.shared.statusBarFrame)
                statusBar.tag = Int(tag)
                UIApplication.shared.keyWindow?.addSubview(statusBar)
                statusBarView = statusBar
            }
        } else {
            statusBarView = (UIApplication.shared.value(forKey: "statusBar") as? UIView)!
            if statusBarView.responds(to: #selector(setter: UIView.backgroundColor)){
                statusBarView.backgroundColor = UIColor.red
            }
        }
-4

if somebody wants to change the battery and text color of the status bar like the below image:

enter image description here

you can use the following code in the appdelegate class.

UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.