在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”而终止应用程序,原因:“ UIApplication上的名为-statusBar或-statusBarWindow的应用程序:由于不再有状态栏或状态栏窗口,因此必须更改此代码。而是在窗口场景上使用statusBarManager对象。'
有什么建议吗?
答案 0 :(得分:4)
尝试一下:
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
}}