我有一个TableCellView,其中包含以下视图:
class MyCell: UITableViewCell {
var headerStackView: UIStackView = {
var stackView = UIStackView()
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.spacing = 0
stackView.contentMode = .scaleToFill
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
var pointsStackView: UIStackView = {
var stackView = UIStackView()
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.spacing = 10
stackView.contentMode = .scaleToFill
stackView.translatesAutoresizingMaskIntoConstraints = false
return stackView
}()
var webContainerView: UIView = {
var webCont = UIView()
webCont.translatesAutoresizingMaskIntoConstraints = false
return webCont
}()
var contentWKWebView: WKWebView = {
let preferences = WKPreferences()
preferences.javaScriptEnabled = true
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
let jsPath = Bundle.main.path(forResource: "web_assets/jqmath-0.4.0", ofType: "css")!
let baseUrl = URL(fileURLWithPath: jsPath)
let html_loading = "<html><body><h1>Loading ...</h1></body></html>"
var wkwv = WKWebView(frame: .zero, configuration: configuration)
wkwv.loadHTMLString(html_loading, baseURL: baseUrl)
wkwv.scrollView.isScrollEnabled = false
wkwv.scrollView.bounces = false
wkwv.translatesAutoresizingMaskIntoConstraints = false
return wkwv
}()
var webViewHeightConstraint: NSLayoutConstraint?
然后,init包含以下几行:
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
print ("Inside INIT")
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.addSubview(self.headerStackView)
self.addSubview(self.pointsStackView)
self.addSubview(self.webContainerView)
self.webContainerView.addSubview(self.contentWKWebView)
headerStackView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 0).isActive = true
headerStackView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: 0).isActive = true
headerStackView.topAnchor.constraint(equalTo: self.topAnchor, constant: 10).isActive = true
headerStackView.bottomAnchor.constraint(equalTo: self.pointsStackView.topAnchor, constant: -10).isActive = true
pointsStackView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 10).isActive = true
pointsStackView.bottomAnchor.constraint(equalTo: self.webContainerView.topAnchor, constant: -10).isActive = true
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
func layoutSubviews只是将一些值设置到各个视图中。
我正在使用以下代码根据HTML内容调整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
})
}
}
两行:
webViewHeightConstraint = contentWKWebView.heightAnchor.constraint(equalToConstant: 100)
webViewHeightConstraint?.isActive = true
克服所有其他限制,我得到:
无法同时满足约束条件
其中有大量冲突约束。
如果我将它们注释掉,则不会有约束冲突,但是WKWebView不会显示任何内容(可能是因为它没有初始化的高度约束。
我想念什么?