使用WKWebView拉动刷新

时间:2018-11-12 18:54:07

标签: 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)
    }
}
`

3 个答案:

答案 0 :(得分:1)

将拉动刷新行为应用于Web视图没有什么特别的,但是请记住,您需要将UIRefreshControl作为子视图添加到Web视图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()
}

提示:,您可以直接使用NSURLRequest,而不是使用URLRequest并将其强制转换为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")!))

答案 1 :(得分:0)

refreshControl回调的webView.scrollView下,将didSet添加到webView

var webView: WKWebView! {
    didSet {
        let refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: #selector(reload(_:)), for: .valueChanged)
        webView.scrollView.refreshControl = refreshControl
    }
}

并添加它以重新加载webView

@objc private func reload(_ refreshControl: UIRefreshControl) {
    refreshControl.endRefreshing()
    webView.reload()
}

答案 2 :(得分:0)

以上答案是正确的,但是要使用 UIRefreshControl ,您需要打开scrollView.bounces = true。因此,新的代码将是:

webView.scrollView.bounces = true
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(ViewController.refreshWebView), for: UIControlEvents.valueChanged)
webView.scrollView.addSubview(refreshControl)

该单独的功能之后:

func refreshWebView(sender: UIRefreshControl) {
    webView.reload()
    sender.endRefreshing()
}