我在表格单元格中使用WKWebView,我需要弄清楚WKWebView内容的高度来管理表格视图的高度,但是我能够达到适当的高度。以下是我的努力
// calling delegate on cell like this
let cell = tableView.dequeueReusableCell(withIdentifier: "TourDetailsOverviewCell", for: indexPath) as! TourDetailsOverviewCell
cell.tourOverviewDescriptionWebView.navigationDelegate = self
cell.tourOverviewDescriptionWebView.scrollView.isScrollEnabled = false
cell.tourOverviewDescriptionWebView.loadHTMLString(self.tourDetails?.tourDetail?.tourOverview?.description ?? "", baseURL: nil)
return cell
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if (self.webViewHeight != 0.0) {
//we already know height, no need to reload cell
return
}
webView.frame.size.height = 0.0
if webView.isLoading == false {
webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
let height = webView.scrollView.contentSize
print("height of webView is: \(height)")
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
//DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.webViewHeight = height as! CGFloat
print("height of webView is: \(self.webViewHeight)")
//self.webViewHeight = height as! CGFloat
//self.containerHeight.constant = height as! CGFloat
})
}
})
self.tableView.reloadRows(at: [IndexPath(row: 1, section: 0)], with: .automatic)
}
任何参考文献也将不胜感激。对不起,我的英语不好