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

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


当前回答

实现preferredStatusBarStyle,如你所述,并调用self.setNeedsStatusBarAppearanceUpdate()在ViewDidLoad和 也在信息。设置UIViewControllerBasedStatusBarAppearance为YES(默认是YES)

目前还不清楚为什么它不起作用。我需要检查代码。另一个建议是 使用viewDidLoad UIApplication.sharedApplication()中的工作代码。statusBarStyle = . lightcontent,并将此更改为默认值,当您查看viewgetdisappear viewWillDisappear。

其他回答

在Swift 4或4.2中

你可以加上vc

preferredStatusBarStyle

并设置返回值为

.lightContent或.default

ex:

override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
}

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

第一步:

打开你的信息。并插入一个名为“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

我已经设置了特定的颜色(在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

对于swift 3

.plist

View controller-based status bar appearance = NO

AppDelegate.swift

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Custom statubar
        UIApplication.shared.isStatusBarHidden = false
        UIApplication.shared.statusBarStyle = .lightContent
        let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
        statusBar.backgroundColor = UIColor.gray

        return true
    }

请在“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
            }
        }