How to set Status Bar Style in Swift 3

2019-01-05 07:43发布

I'm using Xcode 8.0 beta 4.

In previous version, UIViewController have method to set the status bar style

public func preferredStatusBarStyle() -> UIStatusBarStyle

However, I found it changed to a "Get ONLY varaiable" in Swift 3.

public var preferredStatusBarStyle: UIStatusBarStyle { get } 

How can provide the style to use in my UIViewController?

28条回答
SAY GOODBYE
2楼-- · 2019-01-05 08:02

You can also do this in storyboard

  1. Create a new entry in info.plist "View controller-based status bar appearance" set it to "YES".
  2. Go to your storyboard and then select the navigation controller that you want to change. Click on the navigation bar from Storyboard document outline section (left panel on storyboard)
  3. Go to the right panel and click the attributes section
  4. Under the Navigation Bar section you will see style. Select the style you would like (default is for black and black is for white)

You will have to do this for each navigation controller you have. However, any views under that navigation controller will change all the view's status bars style/color to the one you just selected. I find this option better because you can see your results instantly and do not have to add extra lines of code in every view controller.

enter image description here

(Done with Xcode 8.3.3 in an all Swift project)

查看更多
手持菜刀,她持情操
3楼-- · 2019-01-05 08:03

This worked for me

Set View controller-based status bar appearance into NO in plist then In UIViewController viewDidAppear just added the following line

UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)
查看更多
你好瞎i
4楼-- · 2019-01-05 08:03

You can using a bool property named "shouldStatusBarDark" to toggle you status bar color. And you also could update its value to change the status bar color when you scrolling.

 var shouldStatusBarDark = false {
     didSet {
         setNeedsStatusBarAppearanceUpdate()
     }
 }

 override var preferredStatusBarStyle: UIStatusBarStyle {
     return shouldStatusBarDark ? .default : .lightContent
 }

 func scrollViewDidScroll(_ scrollView: UIScrollView) {
     let offSetY = scrollView.contentOffset.y
     if offSetY > 50 {
         UIView.animate(withDuration: 0.4, animations: {
             self.navView.alpha = 1
             self.shouldStatusBarDark = true
         })
     } else {
         UIView.animate(withDuration: 0.4, animations: {
             self.navView.alpha = 0
             self.shouldStatusBarDark = false
         })
     }
 }
查看更多
放我归山
5楼-- · 2019-01-05 08:04

You need to add below key in your Info.plist file:

View controller-based status bar appearance with boolean value set to NO

In your appdelegate class, in didFinishLaunchingWithOptions method before return.

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
    statusBar.backgroundColor = UIColor.red
}
UIApplication.shared.statusBarStyle = .lightContent

change backgroundColor and statusBarStyle as per requirement.

查看更多
Viruses.
6楼-- · 2019-01-05 08:04

Swift 4.0 Please use this code in "didFinishLaunchingWithOptions launchOptions:" Appdelegate class

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
}
查看更多
淡お忘
7楼-- · 2019-01-05 08:06

Please Add the following line into your info.plist then only you can achieve it

"View controller-based status bar appearance = NO"

And add the following snippet into your code and see the output

        UIApplication.shared.statusBarStyle = .lightContent
        UIApplication.shared.statusBarStyle = .default
查看更多
登录 后发表回答