Reputation: 1592
Earlier on Xcode 10 and swift 5 I used to change the status bar color as follows:-
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)
}
}
Now on Xcode 11 & Swift 5.1 I get the following error:-
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '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.'
Any suggestions?
Upvotes: 4
Views: 5078
Reputation: 443
Try this:
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
}}
Upvotes: 5