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条回答
一夜七次
2楼-- · 2019-01-05 07:53

Swift 3 & 4, iOS 10 & 11, Xcode 9 & 10
For me, this method doesn't work:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

when I used to each view controller, but this worked:

  • In file info.list, add row: View controller-based status bar appearance and set to NO

  • Next in appdelegate:

    UIApplication.shared.statusBarStyle = .lightContent
    
查看更多
Anthone
3楼-- · 2019-01-05 07:53

using WebkitView

Swift 9.3 iOS 11.3

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

    @IBOutlet weak var webView: WKWebView!
    var hideStatusBar = true

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setNeedsStatusBarAppearanceUpdate()
        let myURL = URL(string: "https://www.apple.com/")
        let myRequest = URLRequest(url: myURL!)
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red

         webView.load(myRequest)

    }
}

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}
查看更多
混吃等死
4楼-- · 2019-01-05 07:54

Xcode 8.3.1, Swift 3.1

  1. Create a new entry in info.plist "View controller-based status bar appearance" set it to "NO".

  2. Open AppDelegate.swift and add these lines in "didFinishLaunchingWithOptions" method:

application.statusBarStyle = .lightContent

查看更多
再贱就再见
5楼-- · 2019-01-05 07:55

For people looking to change status bar for all viewcontrollers on: iOS 11, Swfit 4 solution is pretty easy.

1) Info.plist add:

View controller-based status bar appearance -> NO

2) Left side of XCode slect project > Targets > Select your project > Under General > Deployment Info > Select Status Bar Style: Light

If you want to change status bar only for one viewcontroller, on viewDidLoad add:

override var preferredStatusBarStyle : UIStatusBarStyle {
    return .lightContent
}
查看更多
戒情不戒烟
6楼-- · 2019-01-05 07:58

There seems to be a small issue about the status bar text colour when dealing with navigation bars.

If you want the .plist entry View controller-based status bar appearance set to YES, it sometimes won't work when you have a coloured nav bar.

For example:

override func viewWillAppear(_ animated: Bool) {
    let nav = self.navigationController?.navigationBar
    nav?.barTintColor = .red
    nav?.tintColor = .white
    nav?.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    setNeedsStatusBarAppearanceUpdate()
}

and

override var preferredStatusBarStyle: UIStatusBarStyle {return .lightContent}

The code above won't work even if you have set the following in the AppDelegate:

UIApplication.shared.statusBarStyle = .lightContent

For those still struggling, apparently it somehow judges if the status bar needs to be light or dark by the styles in the nav bar. So, I managed to fix this by adding the following line in viewWillAppear:

nav?.barStyle = UIBarStyle.black

When the bar style is black, then it listens to your overridden variable. Hope this helps someone :)

查看更多
相关推荐>>
7楼-- · 2019-01-05 07:59

swift 3

if View controller-based status bar appearance = YES in Info.plist

then use this extension for all NavigationController

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

if there is no UINavigationController and only have UIViewController then use Below code:

    extension UIViewController
    {
        override open var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
         }
     }
查看更多
登录 后发表回答