Compare commits
2
Commits
cc30f59ef9
...
93ce2e7210
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93ce2e7210 | ||
|
|
bf7123d9f0 |
@@ -210,6 +210,7 @@ GEM
|
||||
xcpretty (~> 0.2, >= 0.0.7)
|
||||
|
||||
PLATFORMS
|
||||
arm64-darwin-21
|
||||
arm64-darwin-22
|
||||
|
||||
DEPENDENCIES
|
||||
|
||||
@@ -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,61 @@ 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 { hiddenPath in
|
||||
// 完全匹配当前 path,或以该 path + "/" 开头(覆盖子页面)
|
||||
let basePath = hiddenPath.hasSuffix("/") ? String(hiddenPath.dropLast()) : hiddenPath
|
||||
return currentPath == basePath || currentPath.hasPrefix(basePath + "/")
|
||||
}
|
||||
}
|
||||
|
||||
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 +479,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", "/h5/pages/order/detail", "/h5/pages/invoice/detail"])
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user