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

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


当前回答

截至2020年10月,Swift 5, Xcode 12

如果你想把它设置为应用中的所有视图控制器如果你的应用有导航控制器。

你可以在plist文件中这样做,如下所示:

其他回答

Swift 4.2解决方案与NavigationController

第一步:

打开你的信息。并插入一个名为“View controller based status bar appearance”或UIViewControllerBasedStatusBarAppearance的新键到YES,让每个VC使用自己的状态属性。

第二步

在每个VC中,像这样重写preferredStatusBarStyle属性:

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent //.default for black style
}

最后一步

重写自定义NavigationController类中的preferredStatusBarStyle属性:

class NavigationController : UINavigationController {

override var preferredStatusBarStyle : UIStatusBarStyle {

    if let topVC = viewControllers.last {
        //return the status property of each VC, look at step 2
        return topVC.preferredStatusBarStyle  
    }

    return .default
}

斯威夫特3

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to: #selector(setter: UIView.backgroundColor)) {
  statusBar.backgroundColor = UIColor.black
} 

这是为特定的视图控制器设置状态栏背景色的解决方案。

在你的信息。plist你需要将基于视图控制器的状态栏外观定义为任何值。

如果你定义它为YES,那么你应该在每个视图控制器中重写preferredStatusBarStyle函数。

如果你定义它为NO,那么你可以在AppDelegate中使用

UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)

这里有十亿个答案,所以我想为什么不以扩展的形式添加另一个(在@ 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

对我有用的是,在故事板中,转到导航控制器,选择导航栏,单击属性检查器,然后将默认样式改为黑色。就是这样!