这篇文章主要为大家详细介绍了swift实现webview加载网页进度条效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了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()
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:swift版webview加载网页进度条效果
基础教程推荐
猜你喜欢
- swift 字符串String的使用方法 2023-07-05
- R语言数可视化Split violin plot小提琴图绘制方法 2022-12-10
- R语言-如何将科学计数法表示的数字转化为文本 2022-11-23
- swift版webview加载网页进度条效果 2023-07-05
- R包ggtreeExtra绘制进化树 2022-12-14
- R语言基于Keras的MLP神经网络及环境搭建 2022-12-10
- ruby-on-rails-使用Nginx的Rails的多阶段环境 2023-09-21
- asm基础——汇编指令之in/out指令 2023-07-06
- Go web部署报错panic: listen tcp xxxxxxx:8090: bind: cannot assign requested address 2023-09-05
- UEFI开发基础HII代码示例 2023-07-07