0

I'm quite new to Swift, I just want to show a mobile version of a site using Apple's WKWebView but on iPhone X or later where there's a notch, the status bar is not solid which doesn't look good if the website has a sticky navigation bar.

I've tried everything I could find online but nothing worked, so I thought I might post my issue here.

I would like to make the status bar solid white so it matches the sticky navigation bar.

This is my code:

import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {

    var webView: WKWebView!

    override var prefersStatusBarHidden: Bool {
        return true
    }
    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string:"https://www.webthat.nl")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
}

This is what I get on iPhones with a notch:

Click here for image

Adeel Miraj
  • 2,472
  • 3
  • 22
  • 32

1 Answers1

-1

Try adding this code

override func viewWillAppear(_ animated: Bool) {
    self.navigationItem.hidesBackButton = true
    let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
    if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
        statusBar.backgroundColor = UIColor.white
    }
}

Also add this to info.plist file

<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
Yogesh Tandel
  • 1,738
  • 1
  • 19
  • 25