我的答案基于@Zaid Pathan 的答案,但已针对 Swift 4 进行了更新。
class WebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var refreshController:UIRefreshControl!
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
//Your webView initializing
webView.scrollView.bounces = true //DONT FORGET IT!!!
refreshController = UIRefreshControl()
refreshController.bounds = CGRect(x: 0, y: 50, width: refreshController.bounds.size.width, height: refreshController.bounds.size.height)
refreshController.addTarget(self, action: #selector(self.refreshWebView(refresh:)) , for: UIControlEvents.valueChanged)
refreshController.tintColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
webView.scrollView.addSubview(refreshController)
}
@objc func refreshWebView(refresh:UIRefreshControl){
webView.reload()
}
最后,不要忘记停止重新加载:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if refreshController.isRefreshing{
refreshController.endRefreshing()
}
}