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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…