【问题标题】:Scrolling to the bottom of the WKWebView programmatically doesn't work以编程方式滚动到 WKWebView 的底部不起作用
【发布时间】:2020-02-18 09:20:47
【问题描述】:

我有一些遗留代码应该稍作更改以符合 Apple 标准,实际上是为了避免使用 UIWebView 并将其更改为 WKWebView 以便能够定期发布应用程序。

那个遗留部分中专门用于在加载后将页面滚动到底部的代码是这样的,它绝​​对是功能性的:

    public func webViewDidFinishLoad(_ webView: UIWebView) {
        var scrollHeight: CGFloat = webView.scrollView.contentSize.height - webView.bounds.size.height
        if 0.0 > scrollHeight {
            scrollHeight = 0.0
        }
        webView.scrollView.setContentOffset(CGPoint.init(x: 0.0, y: scrollHeight), animated: true)
    }

由于我不得不从应用程序中删除 UIWebView 组件,因此我像这样调整了代码。 添加了 WebKit 导入,使我的类符合 WKNavigationDelegate 协议并像这样设置委托: webView.navigationDelegate = self 并实现了相应的功能如下:

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    var scrollHeight: CGFloat = webView.scrollView.contentSize.height - webView.bounds.size.height
    if 0.0 > scrollHeight {
        scrollHeight = 0.0
    }
    webView.scrollView.setContentOffset(CGPoint.init(x: 0.0, y: scrollHeight), animated: true)
}

现在我不明白当 WKWebView 拒绝将页面滚动到底部时我在哪里出错?

【问题讨论】:

    标签: swift uiscrollview webkit wkwebview


    【解决方案1】:

    找到解决办法,把我用的代码去掉,改成下面的:

        public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
              webView.evaluateJavaScript("var scrollingElement = (document.scrollingElement || document.body); scrollingElement.scrollTop = scrollingElement.scrollHeight;")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-31
      • 2016-01-30
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多