效果圖:
wkwebView.gif
- 目前這個版本有一個問題:發(fā)現(xiàn),在加載百度的時候,如果有的網(wǎng)頁本身帶有導(dǎo)航欄,它的頂部的進(jìn)度條就無效了植阴,如果沒有,則可以實(shí)現(xiàn)類似于微信一樣的進(jìn)度條
實(shí)現(xiàn)代碼:
import UIKit
import WebKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(webView)
view.addSubview(progressView)
view.addSubview(centerButton)
webView.frame = view.bounds
progressView.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.size.width, height: 2)
centerButton.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
centerButton.center = view.center
webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)
webView.addObserver(self, forKeyPath: "title", options: .New, context: nil)
webView.addObserver(self, forKeyPath: "loading", options: .New, context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "estimatedProgress" {
let progress = change!["new"] as! Float
print(progress)
if progress >= 1.0 {
self.progressView.setProgress(progress, animated: true)
UIView.animateWithDuration(0.3, delay: 0.3, options: .CurveEaseInOut, animations: {
self.progressView.alpha = 0.0
}, completion: { (_) in
self.progressView.setProgress(0.0, animated: false)
})
}else {
self.progressView.alpha = 1.0
self.progressView.setProgress(progress, animated: true)
}
}else if keyPath == "loading" {
}
else if keyPath == "title" {
print(webView.title ?? "")
}
}
@objc private func click() {
webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.baidu.com")!))
}
private lazy var webView:WKWebView = {
let webView = WKWebView()
webView.UIDelegate = self
webView.navigationDelegate = self
return webView
}()
private lazy var progressView:UIProgressView = {
let progressView = UIProgressView()
progressView.trackTintColor = UIColor.whiteColor()
progressView.progressTintColor = UIColor.greenColor()
return progressView
}()
private lazy var centerButton:UIButton = {
let centerButton = UIButton()
centerButton.backgroundColor = UIColor.orangeColor()
centerButton.addTarget(self, action: #selector(click), forControlEvents: .TouchUpInside)
centerButton.setTitle("點(diǎn)擊一下", forState: .Normal)
return centerButton
}()
deinit{
removeObserver(self, forKeyPath: "estimatedProgress", context: nil)
removeObserver(self, forKeyPath: "title", context: nil)
removeObserver(self, forKeyPath: "loading", context: nil)
}
}
extension ViewController : WKNavigationDelegate,WKUIDelegate {
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
decisionHandler(.Allow)
}
func webView(webView: WKWebView, decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse, decisionHandler: (WKNavigationResponsePolicy) -> Void) {
decisionHandler(.Allow)
}
func webView(webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
}
func webView(webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
}
//MARK: 加載失敗
func webView(webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: NSError) {
}
//完成加載
func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
}
}