WKwebview没有显示活动指示器

时间:2018-10-14 13:31:40

标签: ios swift progress

我正在尝试将网页加载到WKWebView中。实际上效果很好。但是,我一直无法显示进度指示器(UIActivityIndicaatorView())。我不知道在做什么错。我尝试了很多解决方案。请帮助

    var activityIndicator:UIActivityIndicatorView = UIActivityIndicatorView()
    var webView:WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Setting up webview
        let webConfiguraton = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguraton)
        webView.uiDelegate = self
        view.addSubview(webView)
        let myURL = URL(string:"link")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
        webView.addObserver(self, forKeyPath: #keyPath(WKWebView.isLoading), options: .new, context: nil)
        // setting up activity indicator
        activityIndicator.center = self.view.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.gray
        view.addSubview(activityIndicator)
        activityIndicator.startAnimating()
        // Do any additional setup after loading the view.
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "loading"{
            if webView.isLoading{

            } else {

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

WKWebView似乎没有显示,因为您将其frame的大小设置为.zero。您可以尝试将其设置为:

webView = WKWebView(frame: self.view.frame, configuration: webConfiguraton)