3

On a view controller, i want to set the color of status bar black but I am not able to change it. I am using below code for this purpose.

 func setUpUI() {
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
    }

I have also added the value in info.plist

enter image description here

Mr.Kushwaha
  • 491
  • 5
  • 14
TechChain
  • 8,404
  • 29
  • 103
  • 228
  • 1
    You can't set the color. However, you *can* do two things - put an image behind it and/or set the *"style"* (or find to light or dark. (And sure, you can hide it and by doing that create your own status bar - I wouldn't suggest that.) –  Nov 08 '17 at 05:51
  • Possible duplicate of [Change Status Bar Background Color in Swift 3](https://stackoverflow.com/questions/39802420/change-status-bar-background-color-in-swift-3) – Jack Nov 08 '17 at 10:14

2 Answers2

4

You just override the return value of the status bar, like this:

override var preferredStatusBarStyle: UIStatusBarStyle {
   return .lightContent
}
Thomas Wang
  • 2,233
  • 2
  • 14
  • 24
  • 1
    You can also use the function `setNeedsStatusBarAppearanceUpdate()` and call it when you want to change the status bar color. – Thomas Wang Nov 08 '17 at 06:24
2
Use below code for swift 4 and Xcode 9.2.

1) In

info.plist Set

View controller-based status bar appearance Key to

NO

2) In

AppDelegate

didFinishLaunchingWithOptions

set Below Code

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
Iya
  • 1,888
  • 19
  • 12