在 XCode 7.3.x 中,我更改了 StatusBar 的背景颜色:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
但它似乎不再适用于 Swift 3.0。
我会尝试:
func setStatusBarBackgroundColor(color: UIColor) {
guard let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView else {
return
}
statusBar.backgroundColor = color
}
但它给了我:
this class is not key value coding-compliant for the key statusBar.
关于如何用 XCode8/Swift 3.0 改变它有什么想法吗?
最佳答案
extension UIApplication {
var statusBarView: UIView? {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
return nil
}
}
UIApplication.shared.statusBarView?.backgroundColor = .red
iOS 13 更新
App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.
引用How to change the status bar background color and text color on iOS 13?
关于ios - 在 Swift 3 中更改状态栏背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39802420/