Determine the content size of a WKWebView.取得WKWebView的網頁畫面高度

(WKWebView, content size, evaluateJavaScript)

如果畫面中有原生的元件,也包含網頁
但是需要整畫面滑動,需要知道網頁開啟後的高度

可以參考本文
取得網頁開啟後的高度

在WKWebView的didFinish時
透過evaluateJavaScript的元件可以取到網頁的高度

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
guard let height = result as? CGFloat else { return }
self?.webContainerViewHeight.constant = height
})
}
})

}

可以設定外層webview的constant高度

--

--