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

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


当前回答

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()
    }

}

其他回答

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()
    }

}

点击支持文件组(左边顶部-项目名称)。单击“信息”。点击列表之间的+,就像下面的bundle name。并添加“基于视图控制器的状态栏外观”并将其设置为NO。 然后打开AppDelegate.swift,像这样修改:

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {

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

return true
}

这是它。

另一种非常简单的方法是创建UINavigationController类的扩展。

因为重写preferredStatusBarStyle:方法不会工作,除非我们在UINavigationController类内部做它。

    extension UINavigationController {
        open override var preferredStatusBarStyle: UIStatusBarStyle {
            return .lightContent
        }
    }

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

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}

我在这个问题上遇到了一些麻烦。我不太喜欢在视图中全局改变状态栏的颜色,然后在视图中改变它,然后像接受的答案一样消失。信不信由你,你可以通过在你想要的视图控制器上重写preferredStatusBarStyle来实现。经过很长一段时间,这是我所做的让它工作:

Change View controller-based status bar appearance in your info.plist to YES. Now any full screen view controller can change the status bar style by overriding preferredStatusBarStyle. I specify full screen because this will not work for (non-full screen) modal view controllers, not without setting modal​Presentation​Captures​Status​Bar​Appearance to Yes that is. Also if you have embedded view controllers, like in a navigation controller for example, it will ask the top most view controller for status bar style. Overriding child​View​Controller​For​Status​Bar​Style and passing the embedded view controller is supposed to work but it didn't for me. So I just returned the embedded view controllers preferred status bar as the preferred status bar style. Something like this: override var preferredStatusBarStyle: UIStatusBarStyle { if let topViewController = viewControllers.last { return topViewController.preferredStatusBarStyle } return .default }