iOS WKWebView 检测何时到达底部

iOS WKWebView detect when reaches bottom(iOS WKWebView 检测何时到达底部)

本文介绍了iOS WKWebView 检测何时到达底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 UIViewView 到 WKWebView 的迁移问题,检测到使用 WKWebView 时滚动视图到达底部.在 WKWebView 之前,我使用 UIScrollViewDelegate 检测用户是否通过滚动到 WebView 结束来查看所有内容.如果他这样做了,则启用确认"按钮.iPhone - 知道 UIScrollView 是到达顶部还是底部

I have a migration issue from UIViewView to WKWebView, detecting the Scroll View reached bottom when using WKWebView. Prior to WKWebView I used the UIScrollViewDelegate detecting wether the User had seen all of the content by scrolling till the end of the WebView. If he did, the "confirm" button was enabled. iPhone - knowing if a UIScrollView reached the top or bottom

现在使用 WKWebView 这不再起作用了.我猜原因是,当使用 WKWebView 并加载 html 字符串时,它会缩小视图以完全显示内容.所以我必须通过将视口附加到 html 字符串来设置视口.这以与 UIWebView 相同的方式显示内容,提供 html 字符串,而不设置视口.

Now with WKWebView this doesn't work anymore. I guess the reason is, when using a WKWebView and load a html string, it scales the view down for full visiblity of the content. So I had to set the viewport by appending it to the html string. This displays the content in the same way, the UIWebView did, providing the html string, without setting a viewport.

但是现在加载时的 UIScrollViewDelegate 总是告诉底部已经到达.我猜,WKWebView 加载 html,以完全可见性缩放它,scrollViewDelegate 识别,内容是完全可见的,之后视口进入并向上缩放页面,因此需要垂直滚动来显示全部内容.但是此时,我的确认"按钮已经启用.

But now the UIScrollViewDelegate on load always tells that the bottom already reached. I guess, that the WKWebView loads the html, scales it at full visiblity, the scrollViewDelegate recognizes, that the content was fully visible, after that the viewport comes in and scales the page up, so a vertical scroll is needed to display the full content. But at this time, my "confirm" button is already enabled.

代码片段

override func scrollViewDidScroll(_ scrollView: UIScrollView){
    let scrollViewHeight = scrollView.frame.size.height;
    let scrollContentSizeHeight = scrollView.contentSize.height;
    let scrollOffset = scrollView.contentOffset.y;
    if (scrollOffset + scrollViewHeight == scrollContentSizeHeight)
    {
        self.confirmButton.isEnabled = true; 
    }
}

使用 WKWebView,scrollContentSizeHeight 在加载时始终与 scrollViewHeight 相同,但在 scrollViewDidScroll 委托函数调用多次(不滚动)后,scrollContentSizeHeight 大于实际大小的 scrollViewHeight.

With WKWebView, the scrollContentSizeHeight always is the same as scrollViewHeight on load, but after the scrollViewDidScroll delegate function invokes mulitple times (without scrolling) the scrollContentSizeHeight is larger than the scrollViewHeight at real size.

推荐答案

但是现在加载的 UIScrollViewDelegate 总是告诉底部已经到达.

But now the UIScrollViewDelegate on load always tells that the bottom already reached.

在特定情况下的问题是 UIScrollView 委托在 WKWebView 完全加载之前被调用.

The issue in the particular case is UIScrollView delegate is getting called before the WKWebView is loaded completely.

取一个私有实例变量,检查URL是否加载完毕.

Take one private instance variable to check if the URL is loaded completely or not.

var isURLLoaded = false

确认 WKWebView 委托给您的 viewController.

Confirm WKWebView delegates to your viewController.

webView.scrollView.delegate = self
webView.navigationDelegate = self

并覆盖这些委托方法:

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    if isURLLoaded {
        if scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height) {
            confirmButton.isEnabled = true
        } else if scrollView.contentOffset.y < scrollView.contentSize.height {
            confirmButton.isEnabled = false
        }
    }
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

    isURLLoaded = true
}

这篇关于iOS WKWebView 检测何时到达底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS WKWebView 检测何时到达底部

基础教程推荐