3中更改状态栏背景颜色

3中更改状态栏背景颜色

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

问题描述

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

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

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

07-25 06:17