Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 状态栏背景色不会更改为自定义颜色_Ios_Swift_Colors_Statusbar_Uistatusbar - Fatal编程技术网

Ios 状态栏背景色不会更改为自定义颜色

Ios 状态栏背景色不会更改为自定义颜色,ios,swift,colors,statusbar,uistatusbar,Ios,Swift,Colors,Statusbar,Uistatusbar,当我将状态栏背景颜色更改为原生UIColor.gray时,它会更改。 但当我想使用自定义颜色时,它会变成黑色 UIApplication.shared.statusBarView?.backgroundColor=UIColor.gray-此代码工作正常。状态栏背景色为灰色 UIApplication.shared.statusBarView?.backgroundColor=UIColor(红色:30/255,绿色:30/255,蓝色:30/255,alpha:1)-此代码不正确。状态栏背景颜

当我将状态栏背景颜色更改为原生UIColor.gray时,它会更改。 但当我想使用自定义颜色时,它会变成黑色

UIApplication.shared.statusBarView?.backgroundColor=UIColor.gray
-此代码工作正常。状态栏背景色为灰色


UIApplication.shared.statusBarView?.backgroundColor=UIColor(红色:30/255,绿色:30/255,蓝色:30/255,alpha:1)
-此代码不正确。状态栏背景颜色为黑色

首先,在info.plist文件中设置基于视图控制器的状态栏外观属性No

然后在AppDelegate类的didFinishLaunchingWithOptions方法中添加以下代码

extension UIApplication {
var statusBarView: UIView? {
    if #available(iOS 13.0, *) {
        let tag = 5111
        if let statusBar = self.keyWindow?.viewWithTag(tag) {
            return statusBar
        } else {
            let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
            statusBarView.tag = tag

            self.keyWindow?.addSubview(statusBarView)
            return statusBarView
        }
    } else {
        if responds(to: Selector(("statusBar"))) {
            return value(forKey: "statusBar") as? UIView
        }
    }
    return nil}
}

我希望这会对您有所帮助。

代码没有错误,只是您提供的颜色是深灰色
(R:30,G:30,B:30)
表示
深灰色
,这是
状态栏中使用的颜色
,将此颜色更改为其他颜色,例如
(R:202,G:0,B:42)
,它将显示一种
红色

试一试


UIApplication.shared.statusBarView?.backgroundColor=UIColor(红色:202/255,绿色:0/255,蓝色:42/255,alpha:1)

这对您学习Swift 4有帮助吗。看起来像是一个恶作剧,但很有效

您可以在应用程序启动或视图控制器的ViewDid加载期间为状态栏设置背景色

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
        return true
    }
}


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

}


结果如下:


我不知道为什么,但这是互联网上所有解决方案中唯一对我有效的解决方案。。。为什么改变状态栏颜色这样简单的事情一定如此复杂?苹果,你是认真的吗?@KeghamK。-谢谢你的评论。我一有机会就会检查并更新。