Pull to refresh not working in iOS WebView

前端 未结 6 1924
野趣味
野趣味 2021-01-05 05:37

I\'ve implemented a straight forward WKWebView in iOS.

   var refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: Sel         


        
相关标签:
6条回答
  • 2021-01-05 06:03

    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.

    0 讨论(0)
  • 2021-01-05 06:03

    Try to declare this like below.

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

    Hope this works..

    0 讨论(0)
  • 2021-01-05 06:08

    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()
    }
    
    0 讨论(0)
  • 2021-01-05 06:23

    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.

    0 讨论(0)
  • 2021-01-05 06:23

    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.

    0 讨论(0)
  • 2021-01-05 06:25

    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))
    }
    
    0 讨论(0)
提交回复
热议问题