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

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


当前回答

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

其他回答

适用于基于导航的应用

    var addStatusBar = UIView()
    addStatusBar.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 20);
    addStatusBar.backgroundColor = global().UIColorFromRGB(0x65b4d9)
    self.window?.rootViewController?.view .addSubview(addStatusBar)

在阅读了所有的建议,并尝试了一些事情之后,我可以使用以下步骤让它为特定的视图控制器工作:

第一步:

打开你的信息。并插入一个名为“View controller based status bar appearance”的新键到NO

第二步(只是一个解释,不需要实现这个):

通常我们在应用程序中放入以下代码(_:didFinishLaunchingWithOptions:) 方法,

斯威夫特2

UIApplication.sharedApplication().statusBarStyle = .LightContent

斯威夫特3

UIApplication.shared.statusBarStyle = .lightContent

但那会影响所有viewcontroller的statusBarStyle。

那么,如何让它为特定的viewcontroller工作-最后一步:

打开你想要更改statusBarStyle的视图控制器文件,并将以下代码放在viewWillAppear()中,

斯威夫特2

UIApplication.sharedApplication().statusBarStyle = .LightContent

斯威夫特3

UIApplication.shared.statusBarStyle = .lightContent

同样,为特定的viewController实现viewWillDisappear()方法,并放入以下代码行:

斯威夫特2

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

}

斯威夫特3

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}

这一步将首先更改特定视图控制器的statusBarStyle,然后在特定视图控制器消失时将其更改回默认值。不实现viewWillDisappear()将永久性地将statusBarStyle更改为UIStatusBarStyle的新定义值。LightContent

请在“didFinishLaunchingWithOptions launchOptions:”Appdelegate类中使用此代码

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

iOS 13

 var statusBarView: UIView = UIView()
        if #available(iOS 13.0, *) {
            let tag:UInt64 = 38482458385
            if let statusBar = UIApplication.shared.keyWindow?.viewWithTag(Int(tag)) {
                statusBar.backgroundColor = UIColor.red
                statusBarView = statusBar
            } else {
                let statusBar = UIView(frame: UIApplication.shared.statusBarFrame)
                statusBar.tag = Int(tag)
                UIApplication.shared.keyWindow?.addSubview(statusBar)
                statusBarView = statusBar
            }
        } else {
            statusBarView = (UIApplication.shared.value(forKey: "statusBar") as? UIView)!
            if statusBarView.responds(to: #selector(setter: UIView.backgroundColor)){
                statusBarView.backgroundColor = UIColor.red
            }
        }

适用于swift4中基于特定视图控制器的导航

   let app = UIApplication.shared
   let statusBarHeight: CGFloat = app.statusBarFrame.size.height

   let statusbarView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: statusBarHeight))
   statusbarView.backgroundColor = UIColor.red
   view.addSubview(statusbarView)

如果有人想改变状态栏的电池和文本颜色,如下图所示:

您可以在appdelegate类中使用以下代码。

UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]