在swift中设置状态栏的自定义颜色?

问题描述 投票:3回答:2

在视图控制器上,我想将状态栏的颜色设置为黑色,但我无法更改它。我为此目的使用下面的代码。

 func setUpUI() {
        self.navigationController?.setNavigationBarHidden(true, animated: false)
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
    }

我还在info.plist中添加了值

enter image description here

ios swift statusbar
2个回答
4
投票

您只需覆盖状态栏的返回值,如下所示:

override var preferredStatusBarStyle: UIStatusBarStyle {
   return .lightContent
}

2
投票
Use below code for swift 4 and Xcode 9.2.

1)在

info.plist设置

查看基于控制器的状态栏外观键

没有

2)在

AppDelegate中

didFinishLaunchingWithOptions

设置低于代码

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
    if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
        statusBar.backgroundColor = UIColor.red
    }
    UIApplication.shared.statusBarStyle = .lightContent
© www.soinside.com 2019 - 2024. All rights reserved.