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条回答
【Aperson】
2楼-- · 2019-01-29 22:07

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
    }
}
查看更多
走好不送
3楼-- · 2019-01-29 22:11

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

查看更多
闹够了就滚
4楼-- · 2019-01-29 22:14

You have to set the:

navigationController.navigationBar.barStyle = .black

and the text will appear in white

查看更多
Lonely孤独者°
5楼-- · 2019-01-29 22:18

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

查看更多
你好瞎i
6楼-- · 2019-01-29 22:19

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 }
}
查看更多
smile是对你的礼貌
7楼-- · 2019-01-29 22:24

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