override func preferredStatusBarStyle() -> UIStatusBarStyle {
 return UIStatusBarStyle.LightContent;
}

使用上述代码在任何ViewController中为特定的ViewController设置状态栏颜色为白色,在iOS8中对我来说是行不通的。有什么建议吗?使用UIApplication。shared应用方法,在信息中需要更改后颜色更改。Plist为整个应用程序。

// Change the colour of status bar from black to white
UIApplication.sharedApplication().statusBarStyle = .LightContent

我怎么能改变一些必要的和特定的视图控制器的状态栏颜色?


当前回答

对于没有嵌入navigationViewController的特定ViewController,只需将其添加到ViewController文件中。

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

其他回答

自定义状态栏颜色(iOS11+, Swift4+)

如果您正在寻找如何将状态栏更改为自定义颜色的解决方案,这是可行的解决方案。

let statusBarView = UIView()
view.addSubview(statusBarView)
statusBarView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    statusBarView.topAnchor.constraint(equalTo: view.topAnchor),
    statusBarView.leftAnchor.constraint(equalTo: view.leftAnchor),
    statusBarView.rightAnchor.constraint(equalTo: view.rightAnchor),
    statusBarView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
])
statusBarView.backgroundColor = .blue

我已经设置了特定的颜色(在RGB格式)使用下面的代码在应用程序委托文件:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
. . .

 UIApplication.sharedApplication().statusBarHidden = false
        UIApplication.sharedApplication().statusBarStyle = .LightContent

        let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
        if statusBar.respondsToSelector(Selector("setBackgroundColor:")) {
            statusBar.backgroundColor = UIColor.init(red: 0.1, green: 0.27, blue: 0.60, alpha: 1.0)
        }

. . .
}

您还需要添加以下关键信息。Plist文件:

查看基于控制器的状态栏外观,布尔值设置为NO

这里有十亿个答案,所以我想为什么不以扩展的形式添加另一个(在@ ceure的帮助下)

斯威夫特3

扩展:

extension UIApplication {
    class var statusBarBackgroundColor: UIColor? {
        get {
            return (shared.value(forKey: "statusBar") as? UIView)?.backgroundColor
        } set {
            (shared.value(forKey: "statusBar") as? UIView)?.backgroundColor = newValue
        }
    }
}

实现:

UIApplication.statusBarBackgroundColor = .blue
override func viewWillAppear(animated: Bool) {
    self.navigationController?.navigationBarHidden =  true

    UIApplication.sharedApplication().statusBarHidden = false
    UIApplication.sharedApplication().statusBarStyle = .LightContent

    let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
    if statusBar.respondsToSelector("setBackgroundColor:") {
        statusBar.backgroundColor = UIColor.redColor()
    }

}

我在Swift 5、Swift 4.2中使用这种方式。

在Info.plist中添加下一个值:

UIViewControllerBasedStatusBarAppearance = YES

or

UIViewControllerBasedStatusBarAppearance = NO(查看变化)

UIStatusBarHidden = NO

UIStatusBarStyle = UIStatusBarStyleDefault(或设置为UIStatusBarStyleLightContent,如果你想在启动时看到轻状态栏文本)

然后将代码放置到您想要看到浅色内容的特定视图控制器(要看到深色文本,请将preferredStatusBarStyle设置为. darkcontent)。

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

override func viewDidLoad() {
    super.viewDidLoad()

    if let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as? UIView {
        statusBar.backgroundColor = .sunflowerYellow
    }
}