问题描述
尝试实现刷新WebView URL的选项没有成功,到目前为止我有这个
class ViewController2: UIViewController {
@IBOutlet var webview2: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webview2.load(URLRequest(url: URL(string: "https://coinmarketcap.com/")!))
}
//Pull to Refresh
func scrollViewWillEndDragging(_ scrollView: UIScrollView,withVeLocity veLocity: CGPoint,targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if (scrollView.contentOffset.y < 0){
//reach top
print("Reach Top")
webview2.reload()
}
}
}
谁能告诉我为什么它不起作用?我是新来的
解决方法
在意识到我犯了一些错误后,我找到了解决方案。刷新 WebView 的正确代码是:
class ViewController2: UIViewController {
@IBOutlet var webview2: WKWebView!
@objc let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
webview2.load(URLRequest(url: URL(string: "https://coinmarketcap.com/")!))
webview2.scrollView.bounces = true
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self,action: #selector(ViewController2.refreshWebView),for: UIControl.Event.valueChanged)
webview2.scrollView.addSubview(refreshControl)
}
@objc func refreshWebView(sender: UIRefreshControl) {
webview2.load(URLRequest(url: URL(string: "https://coinmarketcap.com/")!))
sender.endRefreshing()
}
}