swift 添加拉取以刷新WKWebView

dxxyhpgq  于 2023-06-04  发布在  Swift
关注(0)|答案(1)|浏览(139)

我正在尝试添加拉刷新到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编程非常陌生)

7eumitmz

7eumitmz1#

将这些行添加到viewDidload方法

override func viewDidLoad() {
        super.viewDidLoad()
        
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: #selector(refreshWebView(_:)), for: UIControl.Event.valueChanged)
        webView.scrollView.addSubview(refreshControl)
    }

相关问题