UIStatusBarStyle 不适用于 Swift

我正在尝试将 Swift 应用程序中的状态栏颜色改为白色,但是碰壁了。我有3个 ViewController,每个都嵌入在一个 NavigationController 中(这可能是问题所在吗?我已经尝试将代码放在 NavigationController 类中。)我已经尝试了我的 AppRegiate.swift 文件的 did FinishLaunchingWithOptions 中的以下两段代码,但都没有用。

application.statusBarStyle = .LightContent

还有

UIApplication.sharedApplication().statusBarStyle = .LightContent

文档所要说的就是 UIBarButtonStyle 是一个 Int 并且给了我这个 enum 片段,这对我的实现没有任何帮助。

enum UIStatusBarStyle : Int {
case Default
case LightContent
case BlackOpaque
}

我错过了什么?

61635 次浏览

You have two options.

If you want to continue manually setting the style of the status bar, continue doing what you're doing, but you'll need to add the following key to your info.plist file with a value of NO.

View controller-based status bar appearance

Or, if you want to continue to use view controller based status bar appearance, instead of setting the application's statusBarStyle, override the preferredStatusBarStyle property in each view controller for which you'd like to specify a status bar style.

Swift 3

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}

Swift 2

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

You have to set the:

navigationController.navigationBar.barStyle = .black

and the text will appear in white

For iOS9.x and Xcode7, just put this inside AppDelegate.swift:

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


UINavigationBar.appearance().barStyle = .Black


}

This will automatically turn your status bar's style to .Lightcontent for all the view controllers inside a UINavigationController.

(Also, delete View controller-based status bar appearance from Info.plist to suppress the warnings you're probably seeing too!)

Swift 3.0

in AppDelegate.swift didFinishLaunchingWithOptions

UIApplication.shared.statusBarStyle = .lightContent

Info.plist

View controller-based status bar appearance -> NO

Swift 2.2

in AppDelegate.swift didFinishLaunchingWithOptions

UIApplication.sharedApplication().statusBarStyle = .LightContent

Info.plist

View controller-based status bar appearance -> NO

On iOS 9 the following (setStatusBarStyle) is deprecated and you will get a warning if you go that way.

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

If you want all statusBars changed in a single shot try adding the following to your Info.plist. This will also make your launch-screen status bar white. While the code above won't.

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

Don't edit your Info.plist. Add this to your ViewController.swift:

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

In Swift 3, status bar style has changed to a computed property in UIViewController that you can override like this:

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent //or default
}

for me all above dind't work until i add:

self.navigationController?.navigationBar.barStyle = .black;

so:

  1. Set UIViewControllerBasedStatusBarAppearance to YES in .plist
  2. In viewDidLoad call self.setNeedsStatusBarAppearanceUpdate();
  3. Override preferredStatusBarStyle
    override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }
  4. In overrided method i set also the navigationBar.barStyle so final
    for light content:
    override var preferredStatusBarStyle: UIStatusBarStyle { self.navigationController?.navigationBar.barStyle = .black;//or default return .lightContent //or default }
    and for black content use default

The source from here and here.

and if this doesn't work you can try add a UINavigationController extension:

extension UINavigationController
{
override open var preferredStatusBarStyle: UIStatusBarStyle {
if let lastVC = self.viewControllers.last
{
return lastVC.preferredStatusBarStyle
}


return .default
}
}

In Swift 3.0 you can override a getter in ViewController for View controller-based status bar appearance:

override var preferredStatusBarStyle: UIStatusBarStyle {
get { return .lightContent }
}

Strange, using Swift 3.1 & XC8.2.1, but all of the above didn't work.

What I did, is just

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

No Plist, no other stuff. HTH

Step 1. Add to info.plist View controller-based status bar appearance -> NO

Step 2. Add code in method where you need to change status bar color:

UIApplication.shared.statusBarStyle = .lightContent //(or .default)
setNeedsStatusBarAppearanceUpdate()

Key line of code: setNeedsStatusBarAppearanceUpdate()