swift版webview加载网页进度条效果

时间:2022-09-20 17:30:54

本文实例为大家分享了swift版webview加载网页展示的具体代码,供大家参考,具体内容如下

比较简单,直接上代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import UIKit
import WebKit
import SnapKit
 
class CMWebVC:
UIViewController
, WKNavigationDelegate {
 
 var webUrl: String?
 var webView: WKWebView =WKWebView()
 var progressView:UIProgressView = UIProgressView()
 var closeBtn: UIButton!
 
 override func initVC() {
  webView.addObserver(self, forKeyPath:"estimatedProgress", options: NSKeyValueObservingOptions.new, context:nil)
  webView.navigationDelegate =self
 }
 
 deinit {
  webView.removeObserver(self, forKeyPath:"estimatedProgress")
  webView.navigationDelegate =nil
 }
 
 override func viewDidLoad() {
  super.viewDidLoad()
  
  // webview
  view.addSubview(webView)
  webView.snp.makeConstraints { (make)in
   make.width.height.equalToSuperview()
  }
  
  // progressview
  view.addSubview(progressView)
  progressView.snp.makeConstraints { (make)in
   make.width.equalToSuperview()
   make.height.equalTo(3)
   make.top.equalToSuperview()
  }
  progressView.tintColor =UIColor.ColorBgTheme()
  progressView.isHidden =true
  
  // load url
  if webUrl !=nil {
   webView.load(URLRequest(url:URL(string: webUrl!)!))
  }
  
  // shear
  self.showRightItem(image:"nav_share") {
   
  }
 }
 
 override func viewWillAppear(_ animated:Bool) {
  super.viewWillAppear(animated)
  self.closeButton()
 }
 
 override func viewWillDisappear(_ animated:Bool) {
  self.closeBtn.removeFromSuperview()
 }
 
 func closeButton() {
  if self.closeBtn ==nil {
   self.closeBtn =UIButton(frame: CGRect(x:44, y: 0, width:44, height: 44))
   self.closeBtn.setTitle("关闭", for: .normal)
   self.closeBtn.setTitleColor(UIColor.black, for: .normal)
   self.closeBtn.addAction({ (button)in
    self.navigationController!.popViewController(animated:true)
   })
   self.navigationController?.navigationBar.addSubview(self.closeBtn)
  }
 }
 
 override func observeValue(forKeyPath keyPath:String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  // 加载进度
  if keyPath == "estimatedProgress" {
   let newprogress = change?[.newKey]!as! Float
   let oldprogress = change?[.oldKey]as? Float ??0.0
   
   //不要让进度条倒着走...有时候goback会出现这种情况
   if newprogress < oldprogress {
    return
   }
   
   if newprogress == 1 {
    progressView.isHidden =true
    progressView.setProgress(0, animated:false)
   }
   else {
    progressView.isHidden =false
    progressView.setProgress(newprogress, animated:true)
   }
  }
 }
 
 func webView(_ webView:WKWebView, didFinish navigation: WKNavigation!) {
  progressView.isHidden =true
  progressView.setProgress(0, animated:false)
 }
 
 func webView(_ webView:WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
  progressView.isHidden =true
  progressView.setProgress(0, animated:false)
 }
 
 override func navigateBack() {
  if webView.canGoBack {
   webView.goBack()
  }
  else {
   super.navigateBack()
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/tiantianios/article/details/78474778