如何在iOS 13上更改状态栏的背景颜色和文本颜色? [英] How to change the status bar background color and text color on iOS 13?

查看:720
本文介绍了如何在iOS 13上更改状态栏的背景颜色和文本颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

随着 iOS 13 的到来,statusBar的视图不再可通过槽访问:

With the arrival of iOS 13 statusBar's view is no longer accessible trough:

value(forKey: "statusBar") as? UIView

由于:

由于未捕获的异常而终止应用程序 "NSInternalInconsistencyException",原因:应用程序名为-statusBar或 UIApplication上的-statusBarWindow:由于不再有状态栏或状态栏窗口,因此必须更改此代码.使用 而是在窗口场景上使用statusBarManager对象.'

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. Use the statusBarManager object on the window scene instead.'

但是由于keyWindow?.windowScene?.statusBarManager似乎不包含与之相关的任何内容,因此尚不清楚应如何使用它来更改颜色.

But it's not clear how it should be used for changing colours as keyWindow?.windowScene?.statusBarManager does not appear to contain anything related to it.

我正在编译具有(iOS 10,*)兼容性的代码,所以我打算继续使用UIKit.

关于这个主题有什么想法吗?

推荐答案

您可以添加一些条件或使用第一个条件.只需为UIApplication创建一些扩展即可.

You can add some conditions or use first one. Just create some extension for UIApplication.

extension UIApplication {
var statusBarUIView: UIView? {
    if #available(iOS 13.0, *) {
        let tag = 38482
        let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first

        if let statusBar = keyWindow?.viewWithTag(tag) {
            return statusBar
        } else {
            guard let statusBarFrame = keyWindow?.windowScene?.statusBarManager?.statusBarFrame else { return nil }
            let statusBarView = UIView(frame: statusBarFrame)
            statusBarView.tag = tag
            keyWindow?.addSubview(statusBarView)
            return statusBarView
        }
    } else if responds(to: Selector(("statusBar"))) {
        return value(forKey: "statusBar") as? UIView
    } else {
        return nil
    }
  }
}

更新:对不起,我没有足够的时间在实际项目中对其进行测试,但是它可以在"Hello world"应用程序中使用.您可以阅读有关 keyWindow 的更多信息,以及 statusBarFrame ,以使其更好.

UPDATED: Sorry, I don't have enough time to test it in real projects, but it works in "Hello world" app. You can read more info about keyWindow and statusBarFrame in order to make it better.

extension UIApplication {
var statusBarUIView: UIView? {

    if #available(iOS 13.0, *) {
        let tag = 3848245

        let keyWindow = UIApplication.shared.connectedScenes
            .map({$0 as? UIWindowScene})
            .compactMap({$0})
            .first?.windows.first

        if let statusBar = keyWindow?.viewWithTag(tag) {
            return statusBar
        } else {
            let height = keyWindow?.windowScene?.statusBarManager?.statusBarFrame ?? .zero
            let statusBarView = UIView(frame: height)
            statusBarView.tag = tag
            statusBarView.layer.zPosition = 999999

            keyWindow?.addSubview(statusBarView)
            return statusBarView
        }

    } else {

        if responds(to: Selector(("statusBar"))) {
            return value(forKey: "statusBar") as? UIView
        }
    }
    return nil
  }
}

这篇关于如何在iOS 13上更改状态栏的背景颜色和文本颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆