iOS UIActivityIndi​​cator 视图未出现在 WKWebKit 视图中

iOS UIActivityIndicator view not appearing in WKWebKit view

当我第一次 运行 加载应用程序和网页时,加载指示器按预期显示。

然后它会在页面加载时按预期消失。

然后我导航到一个新网页,但指示器没有再次出现

有什么建议吗?

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

        var webView: WKWebView!
        var activityIndicator: UIActivityIndicatorView!

        override func loadView() {

            let screenSize = UIScreen.main.bounds
            let screenWidth = screenSize.width
            let screenHeight = screenSize.height

            webView = WKWebView(frame: CGRect(x:0, y: 0, width:screenWidth, height:screenHeight))
            webView.navigationDelegate = self
            //webView.uiDelegate = self
            view = webView
        }
        override func viewDidLoad() {
            super.viewDidLoad()

            let myURL = URL(string:"https://www.bbc.co.uk")
            let myRequest = URLRequest(url: myURL!)

            webView.load(myRequest)

            activityIndicator = UIActivityIndicatorView()
            activityIndicator.center = view.center
            //activityIndicator.hidesWhenStopped = true
            view.addSubview(activityIndicator)
        }

    override var prefersStatusBarHidden: Bool {
        return true
    }

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print("Page loading")
        activityIndicator.startAnimating()
    }

    func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) {
        print("Page loaded")
         activityIndicator.stopAnimating()
    }
}

请检查下面的代码。你犯的错误是没有更新到 super class 意味着 super.loadView().

import UIKit
import WebKit

class ViewController3: UIViewController, WKUIDelegate, WKNavigationDelegate {

    var webView: WKWebView!
    var activityIndicator: UIActivityIndicatorView!

    override func loadView() {
        super.loadView() 

        let screenSize = UIScreen.main.bounds
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height

        webView = WKWebView(frame: CGRect(x:0, y: 0, width:screenWidth, height:screenHeight))
        webView.navigationDelegate = self
        //webView.uiDelegate = self
        view.addSubview(webView)
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string:"https://www.google.com")
        let myRequest = URLRequest(url: myURL!)

        webView.load(myRequest)

        activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
        activityIndicator.center = view.center
        //activityIndicator.style = .whiteLarge
        activityIndicator.color = .red
        activityIndicator.startAnimating()
        //activityIndicator.hidesWhenStopped = true
        view.addSubview(activityIndicator)
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print("Page loading")
        activityIndicator.startAnimating()
    }

    func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) {
        print("Page loaded")
        activityIndicator.stopAnimating()
    }
}