【问题标题】:How to set Status Bar Style in Swift 3如何在 Swift 3 中设置状态栏样式
【发布时间】:2016-12-09 00:01:53
【问题描述】:

我正在使用 Xcode 8.0 beta 4。

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

public func preferredStatusBarStyle() -> UIStatusBarStyle

但是,我发现它在 Swift 3 中更改为“仅获取变量”。

public var preferredStatusBarStyle: UIStatusBarStyle { get } 

如何提供在我的 UIViewController 中使用的样式?

【问题讨论】:

  • 试试这个 var preferredStatusBarStyle: UIStatusBarStyle = .lightContent

标签: ios swift uiviewcontroller uistatusbar ios-statusbar


【解决方案1】:

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

}

【讨论】:

  • 斯威夫特 9.3?错误的年份,马蒂
  • 我建议不要使用私有 API,例如“statusBar”。 Apple 每次都会对 API 进行更改!当苹果更改该 API 时,您的应用程序可能会崩溃,并且它不会再更改状态栏。这将使您再次搜索其他解决方案。
【解决方案2】:

我也在为此苦苦挣扎,所以如果您正在展示一个全屏模式视图控制器,请确保将 .modalPresentationStyle 设置为 .fullscreen,然后在您展示的视图控制器上,只需将 .preferredStatusBarStyle 覆盖为 .lightContent .

所以:

let navigationController = UINavigationController(rootViewController: viewController)
navigationController.modalPresentationStyle = .fullScreen // <=== make sure to set navigation modal presentation style
present(navigationController, animated: true, completion: nil)

在您的自定义视图控制器上,覆盖状态栏样式:

    override var preferredStatusBarStyle: UIStatusBarStyle {
        .lightContent
    }

【讨论】:

    【解决方案3】:

    斯威夫特 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
        }
    

    【讨论】:

    • 没有帮助。问题是关于状态栏,而不是导航栏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    相关资源
    最近更新 更多