我正在尝试添加拉刷新到webview应用程序。我看过一些教程和许多类似的问题,但我的代码是不同的,我不知道在哪里添加UIRefreshControl
。当我说“我的代码”时,它实际上不是我的代码,而是取自this repo
下面是我的代码:
func createWebView(container: UIView, WKSMH: WKScriptMessageHandler, WKND: WKNavigationDelegate, NSO: NSObject, VC: ViewController) -> WKWebView{
let config = WKWebViewConfiguration()
let webView = WKWebView(frame: calcWebviewFrame(webviewView: container, toolbarView: nil), configuration: config)
webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
webView.isHidden = true;
webView.navigationDelegate = WKND;
webView.scrollView.bounces = true;
return webView
}
extension ViewController: WKUIDelegate {
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
if (navigationAction.targetFrame == nil) {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refreshWebView(_:)), for: UIControl.Event.valueChanged)
webView.scrollView.addSubview(refreshControl)
webView.load(navigationAction.request)
}
return nil
}
@objc func refreshWebView(_ sender: UIRefreshControl) {
print("triggered reload");
MyProject.webView?.reload()
sender.endRefreshing()
}
}
我可以拉页面,但刷新指示器不出现。我错过了什么?
(我对Swift和iOS编程非常陌生)
1条答案
按热度按时间7eumitmz1#
将这些行添加到viewDidload方法