【问题标题】:WKWebview Swift 4 UIProgressBar Doesn't work Xcode 9WKWebview Swift 4 UIProgressBar 不起作用 Xcode 9
【发布时间】:2018-07-12 15:05:54
【问题描述】:

我正在尝试添加一个在 WKWebview 加载时填充的进度条。我已经尝试使用下面的代码。

导入 UIKit 导入 WebKit 类 GmailViewController:UIViewController、WKUIDelegate、WKNavigationDelegate{

@IBOutlet weak var progressView: UIProgressView!
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var webView: WKWebView!
@IBAction func homeBtn(_ sender: Any) {
     self.performSegue(withIdentifier: "HomeScreen", sender: self)
}
override func viewDidLoad() {
    super.viewDidLoad()
    let webConfiguration = WKWebViewConfiguration()
    webConfiguration.websiteDataStore = WKWebsiteDataStore.default()
    let webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.navigationDelegate = self
    webView.uiDelegate = self
    self.webView = webView
    if let webView = self.webView
    {
        view.addSubview(webView)
        webView.translatesAutoresizingMaskIntoConstraints = false
        let height = NSLayoutConstraint(item: webView, attribute: .height, relatedBy: .equal, toItem: self.contentView, attribute: .height, multiplier: 1, constant: 0)
        let width = NSLayoutConstraint(item: webView, attribute: .width, relatedBy: .equal, toItem: self.contentView, attribute: .width, multiplier: 1, constant: 0)
        let offset = NSLayoutConstraint(item: webView, attribute: .top, relatedBy: .equal, toItem: self.contentView, attribute: .top, multiplier: 1, constant: 0)
        view.addConstraints([height, width, offset])
    }
    let myURL = URL(string: "https://gmail.com/mail")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)
    progressView = UIProgressView(progressViewStyle: .default)
    progressView.sizeToFit()
    // Set frame to exact below of navigation bar if available
    progressView.frame = CGRect(x: 0, y: 64, width: self.view.bounds.size.width, height: 20)
    self.view.addSubview(progressView)
    webView.addObserver(self, forKeyPath: #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if keyPath == "estimatedProgress" {
        print("estimatedProgress")
        progressView.progress = Float(webView.estimatedProgress)
    }}
  }

由于某些奇怪的原因,它在运行时返回错误 cannot set event for nil。

【问题讨论】:

  • 您是否将这样的UIProgressBar 添加到UINavigationBar 中?
  • @AndreaMugnaini。不,UI 进度条位于 web 视图上方的单独视图中

标签: ios swift xcode webview


【解决方案1】:

这是您问题的完整工作解决方案。

斯威夫特 4

import UIKit
import WebKit
import PlaygroundSupport

class MyViewController : UIViewController,WKNavigationDelegate,WKUIDelegate {
    var webView : WKWebView!
    var progressView : UIProgressView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let site = "http://google.com"
        let url = URL(string: site)
        let request = URLRequest(url: url!)
        webView = WKWebView(frame: self.view.frame)
        webView.navigationDelegate = self
        webView.uiDelegate = self
        webView.load(request)
        self.view.addSubview(webView)

        progressView = UIProgressView(progressViewStyle: .default)
        progressView.sizeToFit()
        // Set frame to exact below of navigation bar if available
        progressView.frame = CGRect(x: 0, y: 64, width: self.view.bounds.size.width, height: 20)
        self.view.addSubview(progressView)

        webView.addObserver(self, forKeyPath:
            #keyPath(WKWebView.estimatedProgress), options: .new, context: nil)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "estimatedProgress" {
            print("estimatedProgress")
            progressView.progress = Float(webView.estimatedProgress)
        }
    }
}

【讨论】:

  • 您好,感谢您的回复,我已尝试集成您的代码,并且已更新原始帖子中的代码,但仍然无法正常工作。每次我都会收到此错误:“致命错误:在展开可选值时意外发现 nil”。
  • 我已经测试了我的代码,它工作正常,如果你可以分享你的代码,那么我可以检查你的错误。
  • 嗨,我已经更新了我的问题以显示我的代码。如果你能看看就好了。
  • 请将您的 WKWebView NSLayoutConstraint 转换为框架并检查,如果仍然无法正常工作,请检查您的故事板连接。
  • 我们还需要删除deinit上的观察者吗?
猜你喜欢
  • 2018-07-02
  • 2018-12-06
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
相关资源
最近更新 更多