Swift - 更改缺口设备上的状态栏颜色 [英] Swift - Change status bar color on notch devices

查看:44
本文介绍了Swift - 更改缺口设备上的状态栏颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问到并得到了很多回答,但我似乎无法让它发挥作用.

I know this has been asked and answered quite a lot but I can't seem to make it work.

我有一个带有全尺寸 WKWebView 的 UIViewController.一切正常,除了在缺口设备上看起来不合适的状态栏.问题是,没有任何效果,即使是我自己的其他应用程序代码.

I have an UIViewController with a full size WKWebView. Everything works fine, except for the status bar that looks out of place on notch devices. The problem is, nothing works, even my own code from other apps.

到目前为止我尝试过的事情(这在另一个应用程序中工作得很好......):

Things that I tried so far (this works just fine in another app...):

if #available(iOS 13, *)
        {
            let statusBar = UIView(frame: (UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.windowScene?.statusBarManager?.statusBarFrame)!)
            //statusBar.backgroundColor = UIColor(red: 242/255, green: 242/255, blue: 242/255, alpha: 1.0)
            statusBar.backgroundColor = UIColor.red
            UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.addSubview(statusBar)
        }
        else
        {
            let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
            
            if statusBar.responds(to:#selector(setter: UIView.backgroundColor))
            {
                statusBar.backgroundColor = UIColor(red: 242/255, green: 242/255, blue: 242/255, alpha: 1.0)
            }
        } 

还有这个:

extension UIApplication {

    var statusBarUIView: UIView? {

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

            let keyWindow: UIWindow? = UIApplication.shared.windows.filter {$0.isKeyWindow}.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 上的状态栏背景颜色和文本颜色?

在 iOS 13 中更改状态栏颜色?

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

甚至尝试从显示在 WKWebView 中的网页上执行此操作:https://itnext.io/make-your-pwas-look-handsome-on-ios-fd8fdfcd5777

Even tried doing it from my webpage displayed inside the WKWebView: https://itnext.io/make-your-pwas-look-handsome-on-ios-fd8fdfcd5777

解决问题的唯一解决方法是更改​​主视图的背景颜色.问题是,这也会改变底部的颜色.

The only workaround that kinda fixes the issue is to change the background color of the main view. The problem is, this will also change the color of the bottom section.

我什至尝试弄乱 info.plist 文件中的样式.

I even tried messing around with the style in the info.plist file.

对我缺少的东西有什么想法吗?

Any ideas on what I'm missing?

推荐答案

你可以把它添加到你的控制器中,然后你检查 hasNotch

you can just add that in your controller and then you check the hasNotch

extension UIDevice {
    /// Returns `true` if the device has a notch
    var hasNotch: Bool {
        guard #available(iOS 11.0, *),
              let window = UIApplication.shared.windows.first(where: { $0.isKeyWindow }),
              window.safeAreaInsets.bottom >= 10 else {
            return false
        }
        
        return true
    }
}

这篇关于Swift - 更改缺口设备上的状态栏颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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