【问题标题】:How to get WKWebview height while typing in iOS swift如何在 iOS swift 中输入时获取 WKWebview 高度
【发布时间】:2021-09-05 00:36:16
【问题描述】:

我有一个简单的 WKWebview。

首先,一旦加载了 webview,我就可以得到它的高度。

   func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript("document.body.scrollHeight;") { (result, error) in
            if error == nil, let value = result {
                if let height = value as? Int {
                    // found the webview's height here.
                }
            }
        }
   }

但在此之后,我需要在 打字时获取 webview 的高度。如何在 iOS 中快速实现这一点?

【问题讨论】:

  • 在哪里输入?? JS 端还是 Swift 端?
  • 这是一个可编辑的 wkwebview,我们可以在其中输入。所以,在输入 in wkwebview.

标签: ios swift dynamic height wkwebview


【解决方案1】:

无需使用计时器,只需在 wkwebview 中的输入中添加一个事件监听器(see here 怎么做,类似于 iOS 上的textViewDidChange)并在那里调用:

webView.evaluateJavaScript("document.body.scrollHeight;") { (result, error) in
            if error == nil, let value = result {
                if let height = value as? Int {
                    // found the webview's height here.
                }
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-30
    • 2018-12-10
    • 2018-02-13
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多