Pull to refresh not working in iOS WebView

ⅰ亾dé卋堺 提交于 2019-12-19 02:05:52

问题


I've implemented a straight forward WKWebView in iOS.

   var refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: Selector("refreshWebView"), forControlEvents: UIControlEvents.ValueChanged)

This is in viewDidLoad()

And the corresponding refreshWebView function. But when I run it on mobile or simulator, no matter how much I drag, there is no pull to refresh action. Nothing is triggered, and it almost feels like I am stuck to the upper bounds of the screen. Where can be going wrong?


回答1:


Try to change your code like this, If you want to pull to refresh the webView you need to addSubView UIRefreshControl object in the ScrollView of webView like this

let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(self.refreshWebView(_:)), forControlEvents: UIControlEvents.ValueChanged)
webView.scrollView.addSubview(refreshControl)

Add func like this

func refreshWebView(sender: UIRefreshControl) {
    print("refersh")
    webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://google.com")!))
    sender.endRefreshing()
}

Hope this will help you.




回答2:


SWIFT 4 Solution (Based on Nirav D answer)

You can simply call this setup method in your viewDidLoad:

private func setupRefreshControl() {
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(refreshWebView(sender:)), for: UIControlEvents.valueChanged)
    webView.scrollView.addSubview(refreshControl)
}

@objc
private func refreshWebView(sender: UIRefreshControl) {
    print("refreshing...")
    webView.load(URLRequest(url: url))
    sender.endRefreshing()
}



回答3:


Just to say looking at this in Swift 3 and the answer from JAck helped me solve this issue:

in loadView()/viewDidLoad()

webView.scrollView.bounces = true
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(ViewController.refreshWebView), for: UIControlEvents.valueChanged)
webView.scrollView.addSubview(refreshControl)

and then a separate function

func refreshWebView(sender: UIRefreshControl) {
    print("refersh")
    //
    sender.endRefreshing()
}

And I'm now seeing the refresh being printed.




回答4:


You have to enable bouncing for the scrollview of the webview:

webView.scrollView.bounces = true

This will let you drag the webview down which will trigger your UIRefreshControl.




回答5:


Following code will put the refresh control in scrollview of webview.Initially it will load google.com. To see pull to refresh clearly I have set white color to the background of scroll view so it is clearly visible and on pull to refresh webview opens facebook page.

override func viewDidLoad() {

  super.viewDidLoad()
  // Do any additional setup after loading the view, typically from a nib.

    self.refreshControl = UIRefreshControl.init()
    refreshControl!.addTarget(self, action:#selector(refreshControlClicked), for: UIControlEvents.valueChanged)
    self.webView.scrollView.addSubview(self.refreshControl!)

    self.webView.scrollView.backgroundColor = UIColor.white

    self.webView.delegate = self


    let url:URL = URL.init(string:"https://www.google.com")!

    self.loadRequestWithUrl(URLRequest.init(url: url))

}


func webViewDidStartLoad(_ webView: UIWebView) {

    NSLog("website loaded")
}

func loadRequestWithUrl(_ urlRequest : URLRequest?){

    if(urlRequest != nil){

        self.webView.loadRequest(urlRequest!)
    }
}

func refreshControlClicked(){

    let url:URL = URL.init(string:"https://www.facebook.com")!

    self.loadRequestWithUrl(URLRequest.init(url: url))
}



回答6:


Try to declare this like below.

refreshControl.addTarget(self, action: #selector(yourViewController.refreshWebView), forControlEvents: UIControlEvents.ValueChanged)

Hope this works..



来源:https://stackoverflow.com/questions/38156954/pull-to-refresh-not-working-in-ios-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!