研究了一段时间,总算搞定了这个功能封装,现在给大家分享一下,具体看代码,上面有对应的文字描述,有问题请留言,下载源码,请帮忙点一下star,给点继续分享的动力,谢谢~
/**
//swift中的get方法,可以直接varget或letget显示提示,如果使用的是get方法创建视图,调用添加到父类上的时候,要self._参数,如self._wkWebV,如此就可以看出来这个get方法的名称可以跟全局定义的参数名不一样,建议除了“_”不一样外,字母最好一样
//letget
let <#property name#>: <#type name#> = {
<#statements#>
return <#value#>
}()
//varget
var <#variable name#>: <#type#> {
<#statements#>
}
//vargetset
var <#variable name#>: <#type#> {
get {
<#statements#>
}
set {
<#variable name#> = newValue
}
}
**/
//MARK: ------ 创建webView,get方法
var _webV: UIWebView {
webV = UIWebView.init(frame:CGRect.init(x:0, y:Navi_Height, width:Screen_Width, height:Screen_Height-Navi_Height))
webV.delegate = self
if Device_System >= 10.0 && canDownRefresh {
webV.scrollView.refreshControl = self._refreshContr
}
return webV
}
//MARK: ------ 创建WKWebView,get方法
var _wkWebV: WKWebView {
let configWkWeb:WKWebViewConfiguration = WKWebViewConfiguration.init()
configWkWeb.preferences = WKPreferences.init()
configWkWeb.userContentController = WKUserContentController.init()
wkWebV = WKWebView.init(frame: CGRect.init(x: 0, y: Navi_Height, width: Screen_Width, height: Screen_Height-Navi_Height), configuration: configWkWeb)
wkWebV.navigationDelegate = self
wkWebV.uiDelegate = self
//添加此属性可触发侧滑返回上一网页与下一网页操作
wkWebV.allowsBackForwardNavigationGestures = true
//下拉刷新
if Device_System >= 10.0 && canDownRefresh {
wkWebV.scrollView.refreshControl = self._refreshContr
}
//加载进度监听:观察wkwbview的estimatedProgress属性,从而调节进度条
wkWebV.addObserver(self, forKeyPath: "estimatedProgress", options: [NSKeyValueObservingOptions.new], context: nil)
return wkWebV
}
//MARK: ------ 观察者执行的方法
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "estimatedProgress" {
//取值时,这个地方跟OC中字典取值形式不一样,OC中的是change[@"new"]
loadingProgressV.progress = change?[NSKeyValueChangeKey.newKey] as! Float
//print(".......输出数值。。。\(loadingProgressV.progress)")
if loadingProgressV.progress == 1.0 {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double((Int64)(0.4 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: {
self.loadingProgressV.isHidden = true
})
}
}
//print("66666666")
}
//MARK: ------ 创建进度
var _loadingProgressV: UIProgressView {
loadingProgressV = UIProgressView.init(frame: CGRect.init(x: 0, y: Navi_Height, width: Screen_Width, height: 2))
// //如果要添加背景色,需要设置 .bar 样式,然后设置背景色,否则只会默认灰色
// loadingProgressV.progressViewStyle = .bar
// loadingProgressV.backgroundColor = UIColor.red
loadingProgressV.progressTintColor = UIColor.green
return loadingProgressV
}
//MARK: ------ 创建刷新
var _refreshContr: UIRefreshControl {
refreshContr = UIRefreshControl.init()
refreshContr.addTarget(self, action: #selector(webViewReload), for: .valueChanged)
return refreshContr
}
//刷新方法
func webViewReload() {
webV.reload()
wkWebV.reload()
}
//创建button
var _reloadBtn: UIButton {
reloadBtn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 150, height: 150))
reloadBtn.center = self.view.center
reloadBtn.layer.cornerRadius = 75.0
reloadBtn.setBackgroundImage(UIImage.init(named: "placeholder_error"), for: .normal)
reloadBtn.setTitle("您的网络有问题,请检查您的网络设置", for: .normal)
reloadBtn.setTitleColor(UIColor.lightGray, for: .normal)
//跟OC调用不一样了
reloadBtn.titleEdgeInsets = UIEdgeInsetsMake(200, -50, 0, -50)
reloadBtn.titleLabel?.numberOfLines = 0
reloadBtn.titleLabel?.textAlignment = .center
var rect:CGRect = reloadBtn.frame
rect.origin.y -= 100
reloadBtn.frame = rect
reloadBtn.isEnabled = false
return reloadBtn
}
//MARK: ------ 导航按钮
func createNaviItem() {
self.showLeftBarBtnItem()
self.showRightBarBtnItem()
}
//显示左bar
func showLeftBarBtnItem() {
if webV.canGoBack || wkWebV.canGoBack {
self.navigationItem.leftBarButtonItems = [self._backBarBtnItem,self._closeBarBtnItem]
}else{
self.navigationItem.leftBarButtonItem = self._backBarBtnItem
}
}
//显示右bar
func showRightBarBtnItem() {
//这里可以添加一个举报
let rightBarBtn:UIBarButtonItem = UIBarButtonItem.init(title: "举报", style: .plain, target: self, action: #selector(rightBarClick))
self.navigationItem.rightBarButtonItem = rightBarBtn
}
func rightBarClick() {
print("点击了举报。。。。。。")
}
//创建返回bar
var _backBarBtnItem: UIBarButtonItem {
backBarBtnItem = UIBarButtonItem.init(title: "返回", style: .plain, target: self, action: #selector(backBarClick))
return backBarBtnItem
}
func backBarClick() {
if webV.canGoBack || wkWebV.canGoBack {
webV.goBack()
wkWebV.goBack()
}else{
self.navigationController?.popViewController(animated: true)
}
}
//创建关闭bar
var _closeBarBtnItem: UIBarButtonItem {
closeBarBtnItem = UIBarButtonItem.init(title: "关闭", style: .plain, target: self, action: #selector(closeBarClick))
return closeBarBtnItem
}
func closeBarClick() {
self.navigationController?.popViewController(animated: true)
}
效果图:
源码下载(下载源码,请帮忙点一下star,给点继续分享的动力,谢谢~):https://github.com/hbblzjy/SwiftWebControllerDemo