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/

相关文章:

ios - "Failure to deallocate CLLocationManager on the same runloop as its creation may result in a crash"

ios - 覆盖 selectedSegmentIndex

ios - 如何过滤掉太近的坐标?

ios - 将 NSInteger 与排序后的 NSIntegerArray objectAtIndex 进行比较

swift - 如何使用alamofire发送数组参数

ios - 如果用户已创建帐户,则绕过登录

swift - 在两个接口(interface) Controller 之间传递数据

ios - 我想在 swift3 的 tableview 中插入行作为部分

ios - 检测文本字段应该从TableViewController中的Custom UITableViewCell返回以添加新行

ios - 通过 crashlytics 分发的 iOS 版本什么时候到期?