UIStatusBarStyle not working in Swift

2019-01-29 22:01发布

I'm trying to change the Status Bar color in my Swift app to white, but am hitting a brick wall. I have 3 ViewControllers that are each embedded in a NavigationController (could that be the issue? I've already tried to place the code in the NavigationController class.) I've tried both of the following pieces of code in the didFinishLaunchingWithOptions of my AppDelegate.swift file but neither worked.

application.statusBarStyle = .LightContent

and

UIApplication.sharedApplication().statusBarStyle = .LightContent

All that the Docs have to say about it is that UIBarButtonStyle is an Int and gave me this enum snippet which didn't help me at all with implimentation.

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

What am I missing?

11条回答
聊天终结者
2楼-- · 2019-01-29 22:25

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
查看更多
够拽才男人
3楼-- · 2019-01-29 22:25

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/>
查看更多
再贱就再见
4楼-- · 2019-01-29 22:26

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

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}
查看更多
霸刀☆藐视天下
5楼-- · 2019-01-29 22:28

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!)

查看更多
劳资没心,怎么记你
6楼-- · 2019-01-29 22:29

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
}
查看更多
登录 后发表回答