我在UIView中有一个WKWebView。
这些是约束:
webContainerView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
webContainerView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
webContainerView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -10).isActive = true
contentWKWebView.navigationDelegate = self
contentWKWebView.leftAnchor.constraint(equalTo: self.webContainerView.leftAnchor, constant: 2).isActive = true
contentWKWebView.rightAnchor.constraint(equalTo: self.webContainerView.rightAnchor, constant: -2).isActive = true
contentWKWebView.topAnchor.constraint(equalTo: self.webContainerView.topAnchor, constant: 2).isActive = true
contentWKWebView.bottomAnchor.constraint(equalTo: self.webContainerView.bottomAnchor, constant: -2).isActive = true
webViewHeightConstraint = contentWKWebView.heightAnchor.constraint(equalToConstant: 100)
webViewHeightConstraint?.isActive = true // causes error
我必须为WKWebView设置一个初始高度大小,否则自动调整高度以适应内容将无法正常工作。
这是用于自动调整高度的代码:
extension MyCell: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.documentElement.scrollHeight", completionHandler: { (height, error) in
self.webViewHeightConstraint?.constant = height as! CGFloat
})
}
}
UIView和WKWebView是UITableViewCell的子视图。
有什么主意吗?
谢谢!