这是我的代码:
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var progressView: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
webView.uiDelegate = self
webView.navigationDelegate = self
webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (keyPath == "estimatedProgress") {
progressView.setProgress(Float(webView.estimatedProgress), animated: true)
print(webView.estimatedProgress)
}
}
但是,估计进度只显示两个浮点数(0.1和1.0)而且我觉得它不起作用。我使用了Alamofire的进度,它改变了几毫秒,使我的UI更好,但这段代码不能正常工作......
有谁可以帮助我进行webView的进展?
Swift 4.0
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var progressView: UIProgressView!
override func viewDidLoad() {
super.viewDidLoad()
// load website or HTML page
self.webView.load(NSURLRequest(url: URL(string: "https://www.apple.com")!) as URLRequest);
//add observer to get estimated progress value
self.webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil);
}
// Observe value
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress" {
print(self.webView.estimatedProgress);
self.progressView.progress = Float(self.webView.estimatedProgress);
}
}