I want to set the colour of the status bar
(The small strip on top where the clock and battery is displayed) to black background with white text. How can I do this ?
My approach so far :
I added the following in the info.plist
View controller-based status bar appearance --> NO
and in the viewController
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
}
However, the style does not change. How can I make the status bar black?
For Xcode 8 / iOS 10 / Swift 3:
Change the status bar background color temporarily on one view by adding a colored UIView under the status bar. I put these lines in viewWillAppear()
. my tint is white with 45% transparency, but you can put in any opaque or transparent UIColor value.
let statusView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 20))
statusView.backgroundColor = UIColor.white.withAlphaComponent(0.45)
self.view.addSubview(statusView)
You can add your statusView to the navigationController, instead of the view, if you have a navigation controller on the view.
You can also pass in UIScreen.main.bounds.size.width for the width of the simulated status bar background view. Do not hardcode a width, otherwise it wont fit all devices properly.
Alternatively, you can also change the status bar background throughout your app using this code. be aware that if even if placed in viewWillAppear()
, not viewDidLoad()
, other views will still adopt the altered status bar when you navigate forward/back.
let statWindow = UIApplication.shared.value(forKey:"statusBarWindow") as! UIView
let statusBar = statWindow.subviews[0] as UIView
statusBar.backgroundColor = UIColor.white.withAlphaComponent(0.45)