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

2023-11-25

在 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 更新

在 UIApplication 上调用 -statusBar 或 -statusBarWindow 的应用程序:此代码 必须更改,因为不再有状态栏或状态栏 窗户。请改用窗口场景上的 statusBarManager 对象。

参考如何更改iOS 13状态栏背景颜色和文字颜色?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Swift 3 中更改状态栏背景颜色 的相关文章

随机推荐