WKWebView加载完成后,活动指示器不会停止旋转

时间:2019-02-20 14:55:14

标签: ios swift wkwebview uiactivityindicatorview

我对Swift和iOS开发非常陌生,所以在此先非常感谢您对我的帮助!

我已经在线尝试了每个示例以及此站点上的每个页面,并且一旦页面完成在WKWebview中的加载,我就无法停止显示活动指示器。

任何帮助将不胜感激!谢谢!

import UIKit
import WebKit

class FirstViewController: UIViewController,     WKUIDelegate, WKNavigationDelegate {

@IBOutlet var webView: WKWebView!

@IBOutlet var activityIndicator: UIActivityIndicatorView!

override func viewDidLoad() {
    super.viewDidLoad()

    webView.uiDelegate = self

    activityIndicator.startAnimating()
    activityIndicator.isHidden = true
    activityIndicator.hidesWhenStopped = true

    let url = Bundle.main.url(forResource: "Web/bulk_material_table", withExtension: "html")!
    webView.loadFileURL(url, allowingReadAccessTo: url)
    let request = URLRequest(url: url)
    self.webView.load(request)


}

func showActivityIndicator(show: Bool) {
    if show {
        activityIndicator.startAnimating()
    } else {
        activityIndicator.stopAnimating()
    }
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    showActivityIndicator(show: false)
}

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    showActivityIndicator(show: true)
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
    showActivityIndicator(show: false)
}
}

1 个答案:

答案 0 :(得分:1)

只需替换

webView.uiDelegate = self

webView.navigationDelegate = self

它将起作用,因为

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    showActivityIndicator(show: false)
}

WKNavigationDelegate的委托方法。