在 Swift 3 中更改状态栏背景颜色 [英] Change Status Bar Background Color in Swift 3

查看:65
本文介绍了在 Swift 3 中更改状态栏背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 XCode 7.3.x 中我用以下方法更改了我的状态栏的背景颜色:

In XCode 7.3.x ill changed the background Color for my StatusBar with:

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

但这似乎不再适用于 Swift 3.0.

But it seems that this is not working anymore with 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 更改它?

Any Ideas how to change it with 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 对象.

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.

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

这篇关于在 Swift 3 中更改状态栏背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆