feat: 充电管理子页面隐藏原生导航栏
- WebViewController 支持按 URL path 列表自动隐藏原生导航栏 - 监听 webView.url 变化,兼容 SPA 路由切换 - 通过 viewSafeAreaInsetsDidChange 动态调整 WebView 顶部约束 - WebViewTool 为充电管理配置需要隐藏导航栏的子页面 path
This commit is contained in:
@@ -34,16 +34,20 @@ class WebViewController : ZDViewController {
|
||||
|
||||
lazy var statusBarBackgroundView = DDView.init()
|
||||
var showNavBar : Bool
|
||||
/// 需要隐藏原生导航栏的子页面 URL path 列表,由 WebViewTool 按需配置
|
||||
var hiddenNavBarURLPaths: [String]?
|
||||
public var vcTitle : String?
|
||||
var url : String?
|
||||
var screenEdgePanGestureRecognizerEnable : Bool
|
||||
public var disappearHandler: (() -> Void)?
|
||||
|
||||
private let webViewURLKeyPath = "URL"
|
||||
private let locationManager = CLLocationManager()
|
||||
private var locationCallback: String?
|
||||
|
||||
public init(showNavBar:Bool = true,title:String?,url:String,screenEdgePanGestureRecognizerEnable:Bool = true) {
|
||||
public init(showNavBar:Bool = true,title:String?,url:String,screenEdgePanGestureRecognizerEnable:Bool = true, hiddenNavBarURLPaths: [String]? = nil) {
|
||||
self.showNavBar = showNavBar
|
||||
self.hiddenNavBarURLPaths = hiddenNavBarURLPaths
|
||||
self.vcTitle = title
|
||||
self.url = url
|
||||
self.screenEdgePanGestureRecognizerEnable = screenEdgePanGestureRecognizerEnable
|
||||
@@ -55,6 +59,7 @@ class WebViewController : ZDViewController {
|
||||
}
|
||||
|
||||
deinit {
|
||||
webView.removeObserver(self, forKeyPath: webViewURLKeyPath)
|
||||
if disappearHandler != nil {
|
||||
disappearHandler!()
|
||||
}
|
||||
@@ -69,6 +74,7 @@ class WebViewController : ZDViewController {
|
||||
webView.backgroundColor = .white
|
||||
webView.navigationDelegate = self
|
||||
webView.uiDelegate = self
|
||||
webView.addObserver(self, forKeyPath: webViewURLKeyPath, options: .new, context: nil)
|
||||
view.addSubview(webView)
|
||||
|
||||
let request = URLRequest(url: URL(string: url!)!,cachePolicy: .reloadIgnoringLocalAndRemoteCacheData)
|
||||
@@ -95,33 +101,12 @@ class WebViewController : ZDViewController {
|
||||
|
||||
override func viewSafeAreaInsetsDidChange() {
|
||||
super.viewSafeAreaInsetsDidChange()
|
||||
if showNavBar == false {
|
||||
statusBarBackgroundView.snp.remakeConstraints { make in
|
||||
make.top.left.right.equalToSuperview()
|
||||
make.height.equalTo(view.safeAreaInsets.top)
|
||||
}
|
||||
|
||||
webView.snp.remakeConstraints { make in
|
||||
make.left.right.equalToSuperview()
|
||||
make.top.equalTo(statusBarBackgroundView.snp.bottom)
|
||||
make.bottom.equalTo(-view.safeAreaInsets.bottom)
|
||||
}
|
||||
}else{
|
||||
webView.snp.remakeConstraints { make in
|
||||
make.left.right.equalToSuperview()
|
||||
make.top.equalTo(view.safeAreaInsets.top)
|
||||
make.bottom.equalTo(-view.safeAreaInsets.bottom)
|
||||
}
|
||||
}
|
||||
updateWebViewConstraints()
|
||||
}
|
||||
|
||||
override func viewWillAppear(_ animated: Bool) {
|
||||
super.viewWillAppear(animated)
|
||||
if showNavBar == false {
|
||||
navigationController?.navigationBar.isHidden = true
|
||||
}else{
|
||||
dd_navigationBarBackgroundColor = .hex("2C395F")
|
||||
}
|
||||
updateNavBarVisibility()
|
||||
if screenEdgePanGestureRecognizerEnable == false {
|
||||
let nav = navigationController as? DDNavigationController
|
||||
nav?.screenEdgePanGestureRecognizerEnable(false)
|
||||
@@ -149,6 +134,57 @@ class WebViewController : ZDViewController {
|
||||
}
|
||||
}
|
||||
|
||||
private func isNavBarHidden() -> Bool {
|
||||
if showNavBar == false { return true }
|
||||
guard let hiddenPaths = hiddenNavBarURLPaths, let currentUrl = webView.url else { return false }
|
||||
let currentPath = currentUrl.path
|
||||
return hiddenPaths.contains(currentPath)
|
||||
}
|
||||
|
||||
private func updateNavBarVisibility() {
|
||||
let hidden = isNavBarHidden()
|
||||
navigationController?.navigationBar.isHidden = hidden
|
||||
if showNavBar && !hidden {
|
||||
dd_navigationBarBackgroundColor = .hex("2C395F")
|
||||
}
|
||||
// 强制触发布局更新,使 viewSafeAreaInsetsDidChange 能拿到正确的 safeAreaInsets
|
||||
view.setNeedsLayout()
|
||||
view.layoutIfNeeded()
|
||||
}
|
||||
|
||||
private func updateWebViewConstraints() {
|
||||
let hidden = isNavBarHidden()
|
||||
let safeAreaTop = view.safeAreaInsets.top
|
||||
if showNavBar == false || hidden {
|
||||
if statusBarBackgroundView.superview == nil {
|
||||
view.addSubview(statusBarBackgroundView)
|
||||
}
|
||||
statusBarBackgroundView.backgroundColor = .hex("2C395F")
|
||||
statusBarBackgroundView.snp.remakeConstraints { make in
|
||||
make.top.left.right.equalToSuperview()
|
||||
make.height.equalTo(safeAreaTop)
|
||||
}
|
||||
|
||||
webView.snp.remakeConstraints { make in
|
||||
make.left.right.equalToSuperview()
|
||||
make.top.equalTo(statusBarBackgroundView.snp.bottom)
|
||||
make.bottom.equalTo(-view.safeAreaInsets.bottom)
|
||||
}
|
||||
} else {
|
||||
webView.snp.remakeConstraints { make in
|
||||
make.left.right.equalToSuperview()
|
||||
make.top.equalTo(safeAreaTop)
|
||||
make.bottom.equalTo(-view.safeAreaInsets.bottom)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
|
||||
if keyPath == webViewURLKeyPath {
|
||||
updateNavBarVisibility()
|
||||
}
|
||||
}
|
||||
|
||||
private func addScriptMessageHandlers() {
|
||||
let handlers = ["nativeObject", "scan", "getLocation", "openMapNavigation", "callPhone", "openDocument"]
|
||||
handlers.forEach { name in
|
||||
@@ -439,6 +475,7 @@ extension WebViewController : WKNavigationDelegate,WKUIDelegate {
|
||||
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
||||
view.dd_hideHUD()
|
||||
DDLog(message: "finish--------------------------------\(String(describing: webView.url?.absoluteString))")
|
||||
updateNavBarVisibility()
|
||||
if showNavBar == false {
|
||||
statusBarBackgroundView.backgroundColor = .hex("2C395F")
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ open class WebViewTool : NSObject {
|
||||
vc = WebViewController(showNavBar:false, title: nil, url: "\((h5Models?.managerPeople)!)?token=\((USER.token)!)"+(appending ?? ""))
|
||||
break
|
||||
case .gjUrl:
|
||||
vc = WebViewController(showNavBar:true, title: nil, url: "\((h5Models?.gjUrl)!)?"+(appending ?? ""))
|
||||
vc = WebViewController(showNavBar:true, title: nil, url: "\((h5Models?.gjUrl)!)?"+(appending ?? ""), hiddenNavBarURLPaths: ["/h5/pages/order/list", "/h5/pages/invoice/list"])
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user