如何在Swift 3中设置状态栏样式

203

我正在使用Xcode 8.0 beta 4。

在之前的版本中,UIViewController有方法可以设置状态栏样式。

public func preferredStatusBarStyle() -> UIStatusBarStyle

然而,我发现在Swift 3中它变成了一个 "仅获取变量"。

public var preferredStatusBarStyle: UIStatusBarStyle { get } 

我该如何为我的UIViewController提供样式?


请尝试以下代码:var preferredStatusBarStyle: UIStatusBarStyle = .lightContent - Anbu.Karthik
33个回答

0
使用 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
    }

}

3
Swift 9.3?年份错了,马丁。 - iOS Unit
我建议不要使用私有API,例如“statusBar”。苹果公司会不时更改API!当苹果更改该API时,您的应用程序可能会崩溃,并且它将不再更改状态栏。这将使您再次寻找另一种解决方案。 - Egzon P.

0

对于Objective-C,只需在您的应用程序didFinishLaunch方法中添加此行代码即可

UIApplication.sharedApplication.statusBarStyle = UIStatusBarStyleLightContent;

3
自iOS 9起,此功能已被废弃。 - MK_Dev

-1

Swift 3

要在整个应用程序中设置相同的导航栏外观,您可以在AppDelegate.swift中执行以下操作:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        setupNavigationBarAppearence()
        return true
    }



private func setupNavigationBarAppearence(){
        let navigationBarAppearace = UINavigationBar.appearance()
        navigationBarAppearace.isTranslucent = false
        //nav bar color
        navigationBarAppearace.barTintColor = UIColor.primaryColor()
        //SETS navbar title string to white
        navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
        //Makes the batery icon an any other icon of the device to white.
        navigationBarAppearace.barStyle = .black
    }

2
没有帮助。问题是关于状态栏,而不是导航栏。 - Theo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接