ios - Swift:更改 iOS 13 的状态栏颜色

标签 ios swift statusbar

对于 ios 13,我无法设置状态栏的文本颜色。如何获取 statusBarManager 的 View ?如何只更改文本颜色?

由于:

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但这不是我要找的

最佳答案

IOS 13.0 和 XCode 11.0 与 Swift 5.0 100% 工作

    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
    }

关于ios - Swift:更改 iOS 13 的状态栏颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58064283/

相关文章:

ipad - AVURLAsset 拒绝加载视频

ios - 使用 App Extension 的 VPN 连接

swift - 如何在 SwiftUI 中用图像替换占位符文本?

ios - 如何使用webview只显示网页的一部分

ios - Swift:日历应用程序 - 为更多字段着色

ios - setStatusBarHidden :withAnimation doesn't cause subviews to auto-place correctly

android - Lollipop 状态栏上的工具栏阴影

ios - Swift - 检查非托管地址簿单值属性是否为 nil

ios - 最佳实践委托(delegate)和 modalViewControllers

statusbar - 如何在 byobu 中更改 "activity in window"颜色