简体   繁体   English

Swift:更改 iOS 13 的状态栏颜色

[英]Swift: Change status bar color for iOS 13

For ios 13 I can't set text color of the status bar.对于 ios 13,我无法设置状态栏的文本颜色。 How I can get the view of statusBarManager?如何获得 statusBarManager 的视图? How I can change the text color only?如何仅更改文本颜色?

due to:由于:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window.由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“在 UIApplication 上调用 -statusBar 或 -statusBarWindow 的应用程序:必须更改此代码,因为不再有状态栏或状态栏窗口。 Use the statusBarManager object on the window scene instead.'在窗口场景中使用 statusBarManager 对象。

My current code:我当前的代码:

    func setStatusBarTextColor(_ color: UIColor) {
        if #available(iOS 13.0, *) {
            // How to do for iOS 13??
        } else {
            if let statusBar = UIApplication.shared.value(forKey: "statusBar") as? UIView {
                statusBar.setValue(color, forKey: "foregroundColor")
            }
        }
    }

I have already found this https://stackoverflow.com/a/57394751/9172697 but it's not what i looking for我已经找到了这个https://stackoverflow.com/a/57394751/9172697但这不是我要找的

IOS 13.0 and XCode 11.0 with Swift 5.0 100% Working IOS 13.0 和 XCode 11.0 与 Swift 5.0 100% 工作

    if #available(iOS 13.0, *) {


       let statusBar1 =  UIView()
       statusBar1.frame = UIApplication.shared.keyWindow?.windowScene?.statusBarManager!.statusBarFrame as! CGRect
       statusBar1.backgroundColor = UIColor.black

       UIApplication.shared.keyWindow?.addSubview(statusBar1)

    } else {

       let statusBar1: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
       statusBar1.backgroundColor = UIColor.black
    }

You can use like this for iOS 13 :您可以像这样在 iOS 13 中使用:

   let statusBar =  UIView()
   statusBar.frame = UIApplication.shared.statusBarFrame
   statusBar.backgroundColor = UIColor.red
   UIApplication.shared.keyWindow?.addSubview(statusBar)

The color of text in the status bar was never up to you;状态栏中文本的颜色永远不会由您决定; what you were doing was always wrong.你所做的总是错误的。 Use your top level view controller to override preferredStatusBarStyle .使用顶级视图控制器覆盖preferredStatusBarStyle You have two choices, .lightContent and .darkContent , and you should use neither because you want to support light/dark mode.您有两个选择, .lightContent.darkContent ,您不应该使用任何一个,因为您想要支持亮/暗模式。

IOS 15, Xcode 13.2.1, Swift 5 IOS 15、Xcode 13.2.1、Swift 5

I was able to get this to work without errors or warnings using the following:我能够使用以下方法使其正常工作而不会出现错误或警告:

func statusBarColor() {
    if #available(iOS 13.0, *) {
        
        let statusBar2 =  UIView()
        if UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame != nil {
            statusBar2.frame = (UIApplication.shared.currentScene?.statusBarManager!.statusBarFrame)!
            statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
            UIApplication.shared.windows.first?.addSubview(statusBar2)
        }
    } else {
        let statusBar2: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
        statusBar2.backgroundColor = UIColor.init(named: "BackGroundColor")
    }
}

Use: Call the function in viewDidLoad for single scene applications or applications with only a single statusBar color change needed.使用:调用viewDidLoad中的函数,用于单场景应用或只需要改变单个statusBar颜色的应用。 For applications with multiple scenes calling for different statusBar colors I recommend calling the function in viewWillAppear.对于具有多个场景调用不同状态栏颜色的应用程序,我建议在 viewWillAppear 中调用该函数。

UPDATED ANSWER更新的答案

To change the text color on status bar you only need to set the style.要更改状态栏上的文本颜色,您只需要设置样式。 (you don't have to many options, text color in status bar can be white or black) (你不需要太多选项,状态栏中的文本颜色可以是白色或黑色)

If you wan to set status bar style, at view controller level then follow these steps:如果您想设置状态栏样式,请在视图控制器级别执行以下步骤:

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only.如果您只需要在 UIViewController 级别设置状态栏样式,请在 .plist 文件中将UIViewControllerBasedStatusBarAppearance设置为 YES。
  2. override preferredStatusBarStyle in your view controller.覆盖视图控制器中的preferredStatusBarStyle

If you want to change the status bar background for iOS 13 use this code:如果要更改 iOS 13 的状态栏背景,请使用以下代码:

extension UIApplication {

class var statusBarBackgroundColor: UIColor? {
    get {
        return statusBarUIView?.backgroundColor
    } set {
        statusBarUIView?.backgroundColor = newValue
    }
}

class var statusBarUIView: UIView? {
    if #available(iOS 13.0, *) {
        let tag = 987654321

        if let statusBar = UIApplication.shared.keyWindow?.viewWithTag(tag) {
            return statusBar
        }
        else {
            let statusBarView = UIView(frame: UIApplication.shared.statusBarFrame)
            statusBarView.tag = tag

            UIApplication.shared.keyWindow?.addSubview(statusBarView)
            return statusBarView
        }
    } else {
        if responds(to: Selector(("statusBar"))) {
            return value(forKey: "statusBar") as? UIView
        }
    }
    return nil
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM