ios - 在 Swift 3 中更改状态栏背景颜色

标签 ios swift swift3 statusbar uistatusbar

在 XCode 7.3.x 中,我更改了 StatusBar 的背景颜色:

func setStatusBarBackgroundColor(color: UIColor) {
guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView else {
    return
}
statusBar.backgroundColor = color
}

但它似乎不再适用于 Swift 3.0。

我会尝试:

func setStatusBarBackgroundColor(color: UIColor) {
guard  let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView else {
    return
}
statusBar.backgroundColor = color
}

但它给了我:

this class is not key value coding-compliant for the key statusBar.

关于如何用 XCode8/Swift 3.0 改变它有什么想法吗?

最佳答案

extension UIApplication {
    var statusBarView: UIView? {
        if responds(to: Selector(("statusBar"))) {
            return value(forKey: "statusBar") as? UIView
        }
        return nil
    }
}

UIApplication.shared.statusBarView?.backgroundColor = .red

iOS 13 更新

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.

引用How to change the status bar background color and text color on iOS 13?

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

相关文章:

swift - Swift 中的绝对 UTC 偏移量

ios - iPad 2 上的节点位置错误

ios - 在 Firebase 中获取所有用户的名字

iOS 通知。使用哪一个?

ios - 使用 UITextField 中的信息填写 PDF 文档

ios - Realm 无法在后台线程中解密数据库错误

ios - 使用 UIEdgedInset 的自定义 UISlider 渲染效果不佳

ios - Swift 错误无法转换类型 '(AFHTTPRequestOperation?, AnyObject?) 的值 -> ()

ios - 如何将角标(Badge)添加到 UINavigationItem (UIBarButtonItem) - Swift 3

ios - UIViewController没有NavigationBar,UIView与状态栏重叠