在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? { return value(forKey: "statusBar") as? UIView } } UIApplication.shared.statusBarView?.backgroundColor = .red