xcode 10和swift 5我用来更改状态栏颜色,如下所示: -
if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView {
if statusBar.responds(to: #selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = #colorLiteral(red: 0, green: 0.7156304717, blue: 0.9302947521, alpha: 1)
}
}
现在在Xcode 11&Swift 5.1我收到以下错误: -
终止由于未被发现的例外" nsinternalInconSistencyException",原因是:'app:'应用程序称为-statusbar或-statusbarwindow on uiapplication:必须更改此代码,因为不再有状态栏或状态栏窗口。改用窗口场景上的statusbarmanager对象。'
有什么建议?
尝试以下:
extension UIApplication {
class var statusBarBackgroundColor: UIColor? {
get {
return statusBarUIView?.backgroundColor
} set {
statusBarUIView?.backgroundColor = newValue
}
}
class var statusBarUIView: UIView? {
if #available(iOS 13.0, *) {
let tag = 987654321
if let statusBar = UIApplication.shared.keyWindow?.viewWithTag(tag) {
return statusBar
}
else {
let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
statusBarView.tag = tag
UIApplication.shared.keyWindow?.addSubview(statusBarView)
return statusBarView
}
} else {
if responds(to: Selector(("statusBar"))) {
return value(forKey: "statusBar") as? UIView
}
}
return nil
}}