• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iOSWKWebView加载进度条、导航栏返回&关闭(Swift4)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

导航:

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

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift5.4语言指南(七)集合类型发布时间:2022-07-13
下一篇:
使用SwiftPackageManager集成依赖库发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap