Я использую 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)
}
Любая ссылка будет также оценена. извините за мой плохой английский