Simon
Simon

Reputation: 394

iOS UIActivityIndicator view not appearing in WKWebKit view

When I first run the app and the web page is loading the loading indicator shows as expected.

It then disappears as expected when the page loads.

I then navigate to a new web page and the indicator does not reappear

Any suggestions why please?

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()
    }
}

Upvotes: 1

Views: 62

Answers (1)

Sateesh Yemireddi
Sateesh Yemireddi

Reputation: 4399

Please check the code below. The mistake you are doing is not updating to super class means 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()
    }
}

Upvotes: 1

Related Questions