繁体   English   中英

iOS Swift 3 WKWebView进度栏不显示

[英]iOS Swift 3 WKWebView Progress Bar Not Displaying

我正在尝试创建一个iOS WebView应用程序,该应用程序加载网站并允许用户登录并进行购买。 我正在尝试在加载页面时显示进度条,但是进度根本不显示,我不确定哪里出了问题。 我也有刷新视图的功能,但是没有进度条,我也不能确定它是否有效。 这是我的代码,任何帮助将不胜感激!

import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
// Define Views
var webView: WKWebView!
var progressView: UIProgressView!

override func loadView() {
    // Load Initial WebView
    webView = WKWebView()
    webView.navigationDelegate = self
    view = webView
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Create Progress View
    progressView = UIProgressView(progressViewStyle: .default)
    progressView.sizeToFit()
    let progressButton = UIBarButtonItem(customView: progressView)
    toolbarItems = [progressButton]
    navigationController?.isToolbarHidden = false
    //Set and Load Initial URL
    let url = URL(string: "https://shop.nygmarose.com/")!
    webView.load(URLRequest(url: url))
    // Set WebView Config
    webView.allowsBackForwardNavigationGestures = true
    webView.scrollView.isScrollEnabled = true
    webView.scrollView.bounces = true
    // Allow Scroll to Refresh
    let refreshControl = UIRefreshControl()
    refreshControl.addTarget(self, action: #selector(ViewController.refreshWebView), for: UIControlEvents.valueChanged)
    webView.scrollView.addSubview(refreshControl)
    webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
}

func refreshWebView() {
    // On Scroll to Refresh, Reload Current Page
    webView.reload()
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    // Display Progress Bar While Loading Pages
    if keyPath == "estimatedProgress" {
        progressView.progress = Float(webView.estimatedProgress)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}}

我已经检查了您的代码,它工作正常,请检查这里是输出

让我知道更多。

在此处输入图片说明

您是否考虑过强制使用主线程? UI更新仅在主线程上得到保证。

例如:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    // Display Progress Bar While Loading Pages
    if keyPath == "estimatedProgress" {

        // force main thread async call
        DispatchQueue.main.async(execute: {

            // do UI update
            progressView.progress = Float(webView.estimatedProgress)

        })
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM