为什么在Swift中不能使用UIRefreshControl?

我在WKWebview中使用刷新功能。执行刷新时功能正常。但是,当我加载另一个html文件并返回时,它不会刷新。

@IBOutlet var myWebView: WKWebView!
var refController:UIRefreshControl = UIRefreshControl()

override func viewDidLoad() {
    super.viewDidLoad()
    myWebView.uiDelegate = self
    myWebView.navigationDelegate = self
    myWebView.scrollView.delegate = self
    refController.bounds = CGRect.init(x: 0.0,y: 50.0,width: refController.bounds.size.width,height: refController.bounds.size.height)
    refController.addTarget(self,action: #selector(self.webviewRefresh(refresh:)),for: .valueChanged)
    myWebView.scrollView.addSubview(refController)
    ...
}

@objc func webviewRefresh(refresh:UIRefreshControl){
    refController.endRefreshing()
    myWebView.reload()
}

extension mainWebViewController: UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        print("come in ??")
        if currentHtmlFileName == "Main.html" {
            scrollView.bounces = (scrollView.contentOffset.y <= 0)
        } else {
            scrollView.bounces = false
        }
    }
}

从我的代码中可以看到,我的WKWebview正在滚动,并且函数已执行。但是,当在另一个屏幕上激活该函数并返回该函数时,该函数不会被调用,刷新也不会起作用。

重现问题的步骤如下:

  1. 加载WKwebView页面并执行刷新功能。
  2. 滚动到底部屏幕,查看是否有反弹。
  3. 如果正常,请转到其他屏幕。 location.href = "./other.html";
  4. 从另一个屏幕滚动到底部屏幕,看是否有一个 弹跳。
  5. 如果正常没有弹跳,请返回屏幕。 history.back()
  6. 然后再次运行刷新功能。

就我而言,我跳动了,但是刷新没有运行,并且没有调用该函数。

有人解决过与我相同的问题吗?这是一个错误吗?

erfaya 回答:为什么在Swift中不能使用UIRefreshControl?

我对这个问题想了很多。因此,我的结论是history.back()不会改变bounce的值。因此,bounce的值为false

所以我想我应该重新初始化该值。因此,我在完成页面导航的部分中添加了一个功能。

func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) {
     if currentHtmlFileName == "Main.html" {
         scrollViewDidScroll(myWebView.scrollView)
     }
}

添加了此内容后,重新设置了退回值,并且刷新生效。

本文链接:https://www.f2er.com/3037864.html

大家都在问