Pull to refresh not working in iOS WebView

前端 未结 6 1925
野趣味
野趣味 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: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))
    }
    

提交回复
热议问题