【问题标题】:iOS Swift 3 WKWebView Progress Bar Not DisplayingiOS Swift 3 WKWebView 进度条不显示
【发布时间】:2017-07-05 12:44:25
【问题描述】:

我正在尝试创建一个加载站点并允许用户登录和购买的 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.
}}

【问题讨论】:

  • 我已经检查了它的工作是否完美@Musa
  • @iOSDev 它对你有用吗?我想这可能是我的 xcode 模拟器的问题

标签: ios swift xcode progress-bar wkwebview


【解决方案1】:

我已经检查了你的代码,它运行良好,检查这里是输出

让我知道更多。

【讨论】:

  • 谢谢,它没有显示在我的模拟器中,但它一定是我的结局。
【解决方案2】:

你考虑过强制使用主线程吗? 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)

        })
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    相关资源
    最近更新 更多