国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Swift > 正文

swift版webview加載網頁進度條效果

2020-03-09 17:43:42
字體:
來源:轉載
供稿:網友

本文實例為大家分享了swift版webview加載網頁展示的具體代碼,供大家參考,具體內容如下

比較簡單,直接上代碼

import UIKitimport WebKitimport SnapKitclass 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()  } }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到swift教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 册亨县| 奉贤区| 陵水| 井陉县| 泸州市| 远安县| 乐昌市| 静安区| 三亚市| 石林| 滨海县| 屏南县| 中牟县| 若尔盖县| 阜平县| 崇文区| 澄城县| 浠水县| 凌云县| 五常市| 天柱县| 长武县| 安徽省| 武山县| 长武县| 北辰区| 土默特左旗| 贵阳市| 吉林省| 斗六市| 莒南县| 汽车| 辽宁省| 会昌县| 建瓯市| 梓潼县| 五台县| 岫岩| 绍兴县| 长垣县| 农安县|