给WKWebView添加进度条(swift)

在WKWebView上添加进度条比在UIWebView上简单了许多,并且是真的进度了,不用再自己去算比例或者造假的进度条了,

废话少说,进入正题吧:

首先WKWebView有个属性 UIProgressView

1     /** 进度条 */
2     var progressView : UIProgressView? = nil
3     let keyPathForProgress : String = "estimatedProgress"
1 override func viewDidLoad() {
2         super.viewDidLoad()
3 
4         initWebView()
5         initProgressView()
6     }
1 override func viewWillLayoutSubviews() {
2         let width = self.view.bounds.size.width;
3         let height = self.view.bounds.size.height;
4         let statusBarBounds = UIApplication.sharedApplication().statusBarFrame
5         
6         let webViewHeight = height - 64 - 49
7         webView.frame = CGRectMake(0, 64, width, webViewHeight)
8         
9     }
WKWebView代理
1 private func initWebView() {
2         webView.navigationDelegate = self
3         webView.UIDelegate = self
4     }
WKWebView有一个属性estimatedProgress,就是当前网页加载的进度,所以首先监听这个属性。
1 private func initProgressView() {
2 
3         progressView = UIProgressView.init(frame: CGRectMake(0, 0, UILayoutDefine.KScreenWidth, 4))
      // 这里可以改进度条颜色
4 progressView!.tintColor = UIColor.greenColor() 5 webView.addSubview(progressView!)
      //
监听
      webView.addObserver(self, forKeyPath: keyPathForProgress, options: [NSKeyValueObservingOptions.New, NSKeyValueObservingOptions.Old], context: nil) 7 8 }

实现代理方法

 1 /** 计算进度条 */
 2     override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
 3         if ((object?.isEqual(webView) != nil) && (keyPath! == keyPathForProgress) != nil) {
 4             let newProgress = change![NSKeyValueChangeNewKey]?.floatValue
 5             let oldProgress = change![NSKeyValueChangeOldKey]?.floatValue
 6             
 7             if newProgress < oldProgress {
 8                 return
 9             }
10             
11             if newProgress >= 1 {
12                 progressView!.hidden = true
13                 progressView!.setProgress(0, animated: false)
14             } else {
15                 progressView!.hidden = false
16                 progressView!.setProgress(newProgress!, animated: true)
17             }
18         } else {
19             super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
20         }
21     }

 

1 /**
2      移除消息通知
3      */
4     deinit {
5         webView .removeObserver(self, forKeyPath: keyPathForProgress)
6         webView.navigationDelegate = nil
7         webView.UIDelegate = nil
8     }

OK 完成

参考:http://nshipster.cn/wkwebkit/

转载于:https://www.cnblogs.com/Walking-Jin/p/6275067.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值