iOS UIActivityIndi​​cator View 未出现在 WKWebKit View 中

标签 ios uiactivityindicatorview

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

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

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

有什么建议吗?

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

关于iOS UIActivityIndi​​cator View 未出现在 WKWebKit View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53500071/

相关文章:

ios - 在 UITextView 中点击自定义 URL 链接时,委托(delegate)接收到 nil URL

ios - 模态视图不覆盖整个屏幕

ios - 尝试在框架目标中导入 Sinch 时出现 'Include of non-modular header'

ios - 带有 ActivityIndi​​cator 的 NSTimer

iphone - 能够触摸事件指示器的背景

cocoa - 适用于 Mac OS X 的 UIActivityIndi​​catorView?

ios - 取消 UIView 动画 - self.view.layer removeAllAnimations 不工作

iphone - uint32_t 数组的 NSLog 内容

xcode - Swift 事件指示器和标签

swift - 放置 UIActivityIndi​​catorView