亲宝软件园·资讯

展开

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

tiantianios 人气:0
想了解swift版webview加载网页进度条效果的相关内容吗,tiantianios在本文为您仔细讲解swift版webview加载网页的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:swift,webview,加载网页,进度条,下面大家一起来学习吧。

比较简单,直接上代码

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()
  }
 }
}


加载全部内容

相关教程
猜你喜欢
用户评论