UIWebView在ios-11,iPhone-X,Xcode-9中显示重叠状态栏

时间:2018-05-16 06:58:21

标签: xcode uiwebview ios11 iphone-x

我正在使用UIWebView加载一个网站,一切正常,只是iphoneX被切断了我放置“确定”按钮和带标题的标签的栏。

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



    }

任何解决这一僵局的建议。

View image

1 个答案:

答案 0 :(得分:1)

iPhoneX中StatusBar的高度高于其他设备,需要计算此高度并将此值用于WebView坐标。

  1. 添加视图以放置确定按钮:
  2. @IBOutlet weak var myTopBar: UIView!

    1. 计算statusBar的高度:
    2. //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:在webView中,使用此高度:

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