swift版webview加載網(wǎng)頁進(jìn)度條效果
本文實(shí)例為大家分享了swift版webview加載網(wǎng)頁展示的具體代碼,供大家參考,具體內(nèi)容如下
比較簡(jiǎn)單,直接上代碼
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("關(guān)閉", 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?) {
// 加載進(jìn)度
if keyPath == "estimatedProgress" {
let newprogress = change?[.newKey]!as! Float
let oldprogress = change?[.oldKey]as? Float ??0.0
//不要讓進(jìn)度條倒著走...有時(shí)候goback會(huì)出現(xiàn)這種情況
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()
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Swift使用SnapKit模仿Kingfisher第三方擴(kuò)展優(yōu)化
這篇文章主要為大家介紹了Swift?SnapKit模仿Kingfisher第三方擴(kuò)展優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
swift中自定義正則表達(dá)式運(yùn)算符=~詳解
這篇文章主要給大家介紹了關(guān)于swift中自定義正則表達(dá)式運(yùn)算符=~的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Swift實(shí)現(xiàn)監(jiān)聽鍵盤通知及一些處理詳解
這篇文章主要給大家介紹了關(guān)于Swift實(shí)現(xiàn)監(jiān)聽鍵盤通知及一些處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
Swift項(xiàng)目中利用SWRevealViewController實(shí)現(xiàn)側(cè)滑菜單
這篇文章主要介紹了Swift項(xiàng)目中利用SWRevealViewController實(shí)現(xiàn)側(cè)滑菜單,需要的朋友可以參考下2015-12-12
詳解swift中xcworkspace多項(xiàng)目管理
給大家詳細(xì)講解了IOS開發(fā)中swift語言xcworkspace多項(xiàng)目管理的方法和介紹,一起參考一下。2017-11-11
iOS UITableView展開縮放動(dòng)畫實(shí)例代碼
這篇文章主要介紹了Swift UITableView展開縮放動(dòng)畫實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Swift實(shí)現(xiàn)可自定義分頁寬度的UIScrollView
這篇文章主要為大家詳細(xì)介紹了Swift實(shí)現(xiàn)可自定義分頁寬度的UIScrollView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07

