ios - 使用 WKWebView 拉动刷新

标签 ios swift wkwebview pull-to-refresh

我正在尝试将刷新添加到 WKWebView。我应该在这段代码的什么地方添加一些东西来触发拉动刷新?我还没有找到任何可行的方法将其添加到我的代码中,因此您不会在下面的代码中找到我尝试完成此操作的任何尝试。我找到的大多数答案都是使用 Objective-C 或我的项目不支持的语言。

import UIKit
import WebKit

class CycleViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

    var webView: WKWebView!
    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var progressView: UIProgressView!

    override func loadView() {
        super.loadView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        progressView.setProgress(0.1, animated: true)

        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.applicationNameForUserAgent = "Version/8.0.2 Safari/600.2.5"

        let customFrame = CGRect.init(origin: CGPoint.zero, size: CGSize.init(width: 0.0, height: self.containerView.frame.size.height))

        webView = WKWebView(frame: customFrame, configuration: webConfiguration)
        webView.uiDelegate = self
        webView.navigationDelegate = self
        self.containerView.addSubview(webView)
        self.webView.translatesAutoresizingMaskIntoConstraints = false

        self.view.addConstraint(NSLayoutConstraint(item: webView, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailing, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: webView, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leading, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: webView, attribute: .top, relatedBy: .equal, toItem: self.containerView, attribute: .top, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: webView, attribute: .bottom, relatedBy: .equal, toItem: self.containerView, attribute: .bottom, multiplier: 1, constant: 0))



        progressView.sizeToFit()
        progressView.progressTintColor = UIColor(red: 255/255, green: 204/255, blue: 0/255, alpha: 1.0)

        webView.backgroundColor = UIColor.clear
        webView.backgroundColor = UIColor(red: 0.0196, green: 0.4, blue: 0.2902, alpha: 1.0)
        webView.isOpaque = false

        self.webView.load(NSURLRequest(url: URL(string: "https://jwelsh19.wixsite.com/countryday")!) as URLRequest)

        self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)

    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "estimatedProgress" {
            print(self.webView.estimatedProgress);
            self.progressView.progress = Float(self.webView.estimatedProgress);        }
    }

    @IBAction func home(_ sender: UIButton) {
        let myURL = URL(string: "https://jwelsh19.wixsite.com/countryday")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }

    @IBAction func refresh(_ sender: UIButton) {

        webView.reload()

    }

    //This method will call when you press button.
    @objc func acButtonPressed() {

        print("button is pressed")
        self.performSegue(withIdentifier: "Account", sender: self)
    }
}
`

最佳答案

将拉动刷新行为应用于 WebView 没有什么特别之处,但是请记住,您需要将 UIRefreshControl 作为 subview 添加到 WebView scrollView 中:

override func viewDidLoad() {
    // after done with setup the `webView`:
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(reloadWebView(_:)), for: .valueChanged)
    webView.scrollView.addSubview(refreshControl)
}

reloadWebView 方法:

@objc func reloadWebView(_ sender: UIRefreshControl) {
    webView.reload()
    sender.endRefreshing()
}

提示:您可以直接使用 URLRequest 而不是使用 NSURLRequest 并将其转换为 URLRequest的:

self.webView.load(NSURLRequest(url: URL(string: "https://jwelsh19.wixsite.com/countryday")!) as URLRequest)

这样做:

webView.load(URLRequest(url: URL(string: "https://jwelsh19.wixsite.com/countryday")!))

关于ios - 使用 WKWebView 拉动刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53268384/

相关文章:

ios - 如何阻止 SwiftUI 重新加载我通过 UIViewRepresentable 使用的 WKWebView 页面?

ios - WKWebView 白屏(Objective-C)

ios - Expo React iOS 构建在 iPad 中无法正常运行

ios - 在哪里使用 popToRootViewController 将额外的 VC 弹出堆栈?

ios - UIView 没有动画的原因

ios - UIAlertController 子类问题 swift

ios - 如何在iOS中从didReceiveScriptMessage访问WKWebView?

IOS 7.0 : How to change values of layer. 导航按钮的角半径

ios - 在 iOS swift 的谷歌地图中为多边形添加文本标签

ios - 无法识别的选择器发送到实例.. MapView 委托(delegate)的 'NSInvalidArgumentException'