问题
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
回答1:
You just override the return value of the status bar, like this:
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
回答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
来源:https://stackoverflow.com/questions/47172368/set-custom-color-of-status-bar-in-swift