iOS WKWebView检测何时到达底部 [英] iOS WKWebView detect when reaches bottom

查看:64
本文介绍了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.

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

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检测何时到达底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆