Swift:更改iOS 13的状态栏颜色 [英] Swift: Change status bar color for iOS 13
问题描述
对于ios 13,我无法设置状态栏的文本颜色.如何获取statusBarManager的视图?我如何只能更改文本颜色?
For ios 13 I can't set text color of the status bar. How I can get the view of statusBarManager? How I can change the text color only?
由于:
由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:"UIApplication上的名为-statusBar或-statusBarWindow的应用程序:由于不再有状态栏或状态栏窗口,因此必须更改此代码.而是在窗口场景上使用statusBarManager对象.'
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.'
我当前的代码:
func setStatusBarTextColor(_ color: UIColor) {
if #available(iOS 13.0, *) {
// How to do for iOS 13??
} else {
if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView {
statusBar.setValue(color, forKey: "foregroundColor")
}
}
}
我已经找到了这个 https://stackoverflow.com/a/57394751/9172697 ,但这不是我想要的寻找
I have already found this https://stackoverflow.com/a/57394751/9172697 but it's not what i looking for
推荐答案
IOS 13.0和XCode 11.0(使用Swift 5.0 100%工作)
IOS 13.0 and XCode 11.0 with Swift 5.0 100% Working
if #available(iOS 13.0, *) {
let statusBar1 = UIView()
statusBar1.frame = UIApplication.shared.keyWindow?.windowScene?.statusBarManager!.statusBarFrame as! CGRect
statusBar1.backgroundColor = UIColor.black
UIApplication.shared.keyWindow?.addSubview(statusBar1)
} else {
let statusBar1: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
statusBar1.backgroundColor = UIColor.black
}
这篇关于Swift:更改iOS 13的状态栏颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!