0

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

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)
            }
        } 

And this:

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

And everything from here:

How to change the status bar background color and text color on iOS 13?

Change Status Bar Color in iOS 13?

How to set Status Bar Style in Swift 3

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.

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

Any ideas on what I'm missing?

daydr3am3r
  • 920
  • 4
  • 12
  • 31

1 Answers1

0

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
    }
}
Grenoblois
  • 503
  • 1
  • 5
  • 19