UIWebView show overlapping status bar in ios-11, iPhone-X, Xcode-9

扶醉桌前 提交于 2021-01-29 10:55:06

问题


I'm loading a web with UIWebView, everything works fine except that the iphoneX is cut off the bar where I put an "OK" button and a label with a title.

    // webView
    var webView: WKWebView!
override func viewDidLoad() {
    super.viewDidLoad()
    let myURL = URL(string: "https://google.com")
    let myRequest = URLRequest(url: myURL!)

    webView = WKWebView(frame: CGRect( x: 0, y: 60, width: self.view.frame.width, height: self.view.frame.height - 60 ), configuration: WKWebViewConfiguration() )

    //webView.backgroundColor = UIColor.blue
    self.view.addSubview(webView)

    webView.load(myRequest)
    self.webView.allowsBackForwardNavigationGestures = true

    //hide navegation bar
    self.navigationController?.setNavigationBarHidden(true, animated: true)

    // add cornerRadius to view
    navegador.layer.cornerRadius = 10

    //add observer to get estimated progress value
    self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)



    }

Any suggestions to solve this impasse.


回答1:


The height of the StatusBar in the iPhoneX is higher than in the other devices, it is necessary to calculate this height and use this value for the WebView coordinates.

  1. Add a view to place the OK button:

@IBOutlet weak var myTopBar: UIView!

  1. Calculate height of statusBar:

//Get height status bar let statusBarHeight = UIApplication.shared.statusBarFrame.height
// to see correctly on all device models the new height will be: let heightTotal = self.myTopBar.frame.height + statusBarHeight

3: In the webView, use this height:

webView = WKWebView(frame: CGRect( x: 0, y: heightTotal, width: self.view.frame.width, height: self.view.frame.height - heightTotal), configuration: WKWebViewConfiguration() )


来源:https://stackoverflow.com/questions/50364274/uiwebview-show-overlapping-status-bar-in-ios-11-iphone-x-xcode-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!