码迷,mamicode.com
首页 > 移动开发 > 详细

iOS WKWebView 加载进度条、导航栏返回&关闭 (Swift 4)

时间:2017-10-09 10:58:58      阅读:1307      评论:0      收藏:0      [点我收藏+]

标签:没有   span   dispatch   nim   sel   hidden   idf   tco   nil   

 

 

导航:

1.加载进度条

2.导航栏增加返回、关闭按钮

 

 

 


 

 

 加载进度条

 

 

效果图

技术分享

 

 

 

代码如下:

self.progressView.trackTintColor = UIColor.white
self.progressView.progressTintColor = UIColor.colorWithHex(value: 0x72A438, alpha: 1).withAlphaComponent(0.8)
self.progressView.frame = CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 1)
self.progressView.transform = CGAffineTransform.init(scaleX: 1, y: 3)
self.view.addSubview(self.progressView)
self.progressView.progress = 0.05 //设置初始值,防止网页加载过慢时没有进度

// Swift 4 KVO
self.observeBlock = webView.observe(\.estimatedProgress, options: NSKeyValueObservingOptions.new.union(NSKeyValueObservingOptions.old)) { (webview, changed) in
    
    if let new = changed.newValue{
        self.progressView.isHidden = false
        self.progressView.progress = Float(new)
        if new >= 1.0{
            delay(second: 0.3, block: {
                self.progressView.isHidden = true
            })
        }
    }
}

 

 

// 加载失败隐藏进度条
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
    
    self.progressView.isHidden = true
}

 

 

延迟执行函数:

func delay(second:Double,block:@escaping (() -> Void)){
    
    DispatchQueue.main.asyncAfter(deadline: .now() + second, execute: {
        block()
    })
}

  

 

 

 


 

  

导航栏增加返回、关闭按钮

 

 

效果图

技术分享

 

 

代码如下:

func showLeftNavigationItem(){
    
    let goBackBtn = UIButton.init()
    let closeBtn = UIButton.init()
    
    goBackBtn.setImage(UIImage.init(named: "navi_go_back"), for: UIControlState.normal)
    goBackBtn.setTitle(" 返回", for: UIControlState.normal)
    goBackBtn.addTarget(self, action: #selector(goBack), for: UIControlEvents.touchUpInside)
    goBackBtn.sizeToFit()
    goBackBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 8)
    
    let backItem = UIBarButtonItem.init(customView: goBackBtn)
    closeBtn.setTitle("关闭", for: UIControlState.normal)
    closeBtn.addTarget(self, action: #selector(popViewController), for: UIControlEvents.touchUpInside)
    closeBtn.sizeToFit()
    let closeItem = UIBarButtonItem.init(customView: closeBtn)
    
    let items:[UIBarButtonItem] = [backItem,closeItem]
    self.navigationItem.leftBarButtonItems = items
}

 

按钮对应的Selector:

@objc func goBack(){
	self.webView.goBack()
}
@objc func popViewController(){
    
    self.navigationController?.popViewController(animated: true)
}

  

 

加载完成时,决定是否显示这两个按钮:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    
    checkGoBack()
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
    
    checkGoBack()
}
/// 检查返回(pop/goback)
func checkGoBack(){
    
    self.navigationController?.interactivePopGestureRecognizer?.isEnabled = !self.webView.canGoBack
    if self.webView.canGoBack{
        showLeftNavigationItem()
    }else{
        self.navigationItem.leftBarButtonItems = nil
    }
}

  

 

 

 

白色返回箭头图片: 

右键括号处保存    2x:(技术分享)       3x:(技术分享)

 

 

 

 


 Ficow原创,转载请注明出处:http://www.cnblogs.com/ficow/p/7639912.html

 

iOS WKWebView 加载进度条、导航栏返回&关闭 (Swift 4)

标签:没有   span   dispatch   nim   sel   hidden   idf   tco   nil   

原文地址:http://www.cnblogs.com/ficow/p/7639912.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!