简体   繁体   English

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

[英]Why do not working UIRefreshControl in Swift?

I use the refresh function in WKWebview .我在WKWebview使用刷新功能。 The function is normal when performing a refresh.执行刷新时该功能正常。 But when I load another html file and come back, it is not refreshed.但是当我加载另一个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
        }
    }
}

As you can see from my code, my WKWebview is scrolling, and the function is executed.从我的代码中可以看出,我的WKWebview正在滚动,并且执行了该功能。 However, when the function is activated on a different screen and returned, the function is not called, nor is the refresh working.但是,当该函数在不同的屏幕上被激活并返回时,该函数不会被调用,刷新也不会起作用。

The steps for reproducing a problem are as follows:重现问题的步骤如下:

  1. Load the WKwebView page and execute the refresh function.加载 WKwebView 页面并执行刷新功能。
  2. Scroll to the bottom screen to see if there is a bounce.滚动到底部屏幕以查看是否有反弹。
  3. If it works normally, go to a different screen.如果它正常工作,请转到其他屏幕。 location.href = "./other.html";
  4. Scroll from another screen to the bottom screen to see if there is a bounce.从另一个屏幕滚动到底部屏幕以查看是否有反弹。
  5. If there is no bounce normally, please return to the screen.如果没有正常反弹,请返回屏幕。 history.back()
  6. And run the refresh function again.并再次运行刷新功能。

In my case, I have a bounce, but the refresh does not run, and the function is not called.在我的情况下,我有一个反弹,但刷新没有运行,并且没有调用该函数。

Has anyone solved the same problem as me?有没有人解决过和我一样的问题? Is this a bug???这是bug吗???

I thought a lot about this problem.我想了很多关于这个问题。 So my conclusion is history.back() does not change the value of the bounce .所以我的结论是history.back()不会改变bounce的价值。 Therefore, the bounce value is false .因此, bounce值为false

So I thought I should reinitialize this value.所以我想我应该重新初始化这个值。 So I added a function in the section that completes page navigation.所以我在完成页面导航的部分添加了一个功能。

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

With this addition, the bounce value was reinitialized, and my refresh worked.有了这个添加,反弹值被重新初始化,我的刷新工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM