【问题标题】:Unable to update height constraint of WKWebView - Autolayout无法更新 WKWebView 的高度约束 - 自动布局
【发布时间】:2019-09-10 10:04:40
【问题描述】:

我在垂直 UIStackview 中添加了 UILabel 和 WKWebView 并且分布设置为fillProportionally。我正在尝试根据内容高度设置 Web 视图高度。为此,我将初始 Web 视图高度设置为 0。加载 html 内容后,我尝试将 Web 视图的高度约束更新为滚动高度。但我遇到了自动布局约束冲突问题。

//在UIStackView中添加WKWebView

let headerString = "<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></header>"
        let htmlString = headerString + correctAnswer
        questionWebView.loadHTMLString(htmlString, baseURL: nil)
        stackView.addArrangedSubview(questionWebView)
        questionWebView.translatesAutoresizingMaskIntoConstraints = false;
        questionWebView.trailingAnchor.constraint(equalTo: stackView.trailingAnchor).isActive = true
        questionWebView.leadingAnchor.constraint(equalTo: stackView.trailingAnchor).isActive = true
        questionWebVieHeightAnchor = questionWebView.heightAnchor.constraint(equalToConstant: 0.0)
        questionWebVieHeightAnchor.isActive = true

我尝试在 webView didFinish 方法中动态更新高度约束。但我遇到了布局冲突问题。

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
                    print("Height is \(height as! CGFloat)")
                    self.questionWebVieHeightAnchor.constant = height
                    self.questionWebView.layoutIfNeeded()
                })
            }
        })
    }

问题:

   Unable to simultaneously satisfy constraints.
        Probably at least one of the constraints in the following list is one you don't want. 
        Try this: 
            (1) look at each constraint and try to figure out which you don't expect; 
            (2) find the code that added the unwanted constraint or constraints and fix it. 
    (
        "<NSLayoutConstraint:0x600000a194a0 WKWebView:0x7f9363b5fa00.height == 200>",
        "<NSLayoutConstraint:0x600000ad2b70 WKWebView:0x7f9363b5fa00.height == 0>"
    )

    Will attempt to recover by breaking constraint 
    <NSLayoutConstraint:0x600000a194a0 WKWebView:0x7f9363b5fa00.height == 200>

【问题讨论】:

  • 第一个块的写入位置,确保约束没有被添加两次
  • ``` questionWebVieHeightAnchor = questionWebView.heightAnchor.constraint(equalToConstant: 0.0) ``` 我正在尝试用新值更新约束。在 didFinish 导航方法中
  • 我的意思是你在哪个函数中写了这个
  • 我在 UIView 的 init 方法中添加了这个。基本上,我正在尝试在弹出窗口中加载 webview。所以我创建了一个自定义视图并在 init 方法中,我试图加载 webview 和标签
  • @andromedainiative:同意。我尝试删除 Stackview 并直接在 UIView 下添加。现在我可以无冲突地更新 webview 高度了。

标签: ios swift


【解决方案1】:

恐怕这不会很好用。这是因为当您将视图添加到堆栈视图时,堆栈视图将尝试对自身的约束进行排序。基于您添加的视图的内在内容大小。如果您需要更新 Web 视图内容大小,我可能不会使用堆栈视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    相关资源
    最近更新 更多