From 47157d09fa917c0dc25661707f50837b4fac53fe Mon Sep 17 00:00:00 2001 From: "xiaoyou.qu" Date: Sat, 6 Jan 2024 00:21:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=AE=BF=E9=97=AE=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E7=AC=A6=E5=8F=B7,=20=E6=8B=93=E5=B1=95=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E9=BB=98=E8=AE=A4=E8=AE=BE=E7=BD=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XYNav/Example/XYNav/AppDelegate.swift | 1 + .../Classes/XYContentNavController.swift | 6 +- .../Classes/XYNavigationController.swift | 92 ++++++++++++------- 3 files changed, 67 insertions(+), 32 deletions(-) diff --git a/XYNav/Example/XYNav/AppDelegate.swift b/XYNav/Example/XYNav/AppDelegate.swift index 794b700..1ca0db5 100644 --- a/XYNav/Example/XYNav/AppDelegate.swift +++ b/XYNav/Example/XYNav/AppDelegate.swift @@ -20,6 +20,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // XYNavigationController.setDefaultBackImage(UIImage(named: "ic_risk_tip_close")!) XYNavigationController.setDefaultBackImage(UIImage(named: "icon_back")!) + XYNavigationController.nav_setGlobal(showClassNameInNavbar: true, navBarTintColor: .systemPink) return true // UINavigationBar.appearance().compactScrollEdgeAppearance = UINavigationBarAppearance() diff --git a/XYNav/XYNav/Classes/XYContentNavController.swift b/XYNav/XYNav/Classes/XYContentNavController.swift index 431c47e..f940e83 100644 --- a/XYNav/XYNav/Classes/XYContentNavController.swift +++ b/XYNav/XYNav/Classes/XYContentNavController.swift @@ -22,12 +22,16 @@ class XYContentNavController: UINavigationController { } else {} navigationBar.addSubview(statusBar) + + if let globalBarTintColor = XYNavigationController.navBarDefaultColor { + navigationBar.barTintColor = globalBarTintColor + } } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) #if DEBUG - if let vc = topViewController { + if let vc = topViewController, XYNavigationController.showClassNameInNavBar { statusBar.setMsg(with: vc) } #endif diff --git a/XYNav/XYNav/Classes/XYNavigationController.swift b/XYNav/XYNav/Classes/XYNavigationController.swift index 1df23db..ba32e78 100644 --- a/XYNav/XYNav/Classes/XYNavigationController.swift +++ b/XYNav/XYNav/Classes/XYNavigationController.swift @@ -7,7 +7,7 @@ import UIKit -func warpNewPushVC(_ desVC: UIViewController, _ superNav: XYNavigationController) -> UIViewController { +fileprivate func warpNewPushVC(_ desVC: UIViewController, _ superNav: XYNavigationController) -> UIViewController { if desVC is XYContentController { return desVC } let contVC = XYContentController() @@ -23,7 +23,7 @@ func warpNewPushVC(_ desVC: UIViewController, _ superNav: XYNavigationController return contVC } -func warpVC(_ desVC: UIViewController, _ superNav: XYNavigationController, isRootVC: Bool) -> UIViewController { +fileprivate func warpVC(_ desVC: UIViewController, _ superNav: XYNavigationController, isRootVC: Bool) -> UIViewController { let newVC = warpNewPushVC(desVC, superNav) if isRootVC{ return newVC @@ -34,7 +34,7 @@ func warpVC(_ desVC: UIViewController, _ superNav: XYNavigationController, isRoo } } -func unWarpNewPushVC(_ desVC: UIViewController, needResign: Bool) -> UIViewController { +fileprivate func unWarpNewPushVC(_ desVC: UIViewController, needResign: Bool) -> UIViewController { if desVC is XYContentController, let contentVC = desVC as? XYContentController { let resultVC = contentVC.contentVc if needResign { @@ -47,8 +47,8 @@ func unWarpNewPushVC(_ desVC: UIViewController, needResign: Bool) -> UIViewContr return desVC } -var backImage: UIImage? = nil -func getBackImage() -> UIImage { +fileprivate var backImage: UIImage? = nil +fileprivate func getBackImage() -> UIImage { if backImage != nil { return backImage! @@ -71,14 +71,13 @@ func getBackImage() -> UIImage { return image ?? UIImage() } -open -class XYNavigationController: UINavigationController { +public class XYNavigationController: UINavigationController { - // MARK: - open vars + // MARK: - public vars var panGesture: UIPanGestureRecognizer? // MARK: - life circle - open override func viewDidLoad() { + public override func viewDidLoad() { super.viewDidLoad() super.interactivePopGestureRecognizer?.isEnabled = false @@ -109,13 +108,13 @@ class XYNavigationController: UINavigationController { } // MARK: - push & pop - open override func pushViewController(_ viewController: UIViewController, animated: Bool) { + public override func pushViewController(_ viewController: UIViewController, animated: Bool) { var currentVCs = viewControllers currentVCs.append(viewController) setViewControllers(currentVCs, animated: animated) } - open override func popViewController(animated: Bool) -> UIViewController? { + public override func popViewController(animated: Bool) -> UIViewController? { let popVC = super.popViewController(animated: animated) if let resultVC = popVC as? XYContentController { return unWarpNewPushVC(resultVC, needResign: false) @@ -123,7 +122,7 @@ class XYNavigationController: UINavigationController { return nil } - open override func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? { + public override func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? { if self.viewControllers.contains(viewController) == true { // 在自己vc栈中执行 let oldSelfViewControllers = self.viewControllers @@ -153,7 +152,7 @@ class XYNavigationController: UINavigationController { return nil } - open override func popToRootViewController(animated: Bool) -> [UIViewController]? { + public override func popToRootViewController(animated: Bool) -> [UIViewController]? { if self.viewControllers.isEmpty { return nil } @@ -171,7 +170,7 @@ class XYNavigationController: UINavigationController { } return vc } - open override func setViewControllers(_ viewControllers: [UIViewController], animated: Bool) { + public override func setViewControllers(_ viewControllers: [UIViewController], animated: Bool) { var warpedVCs: [UIViewController] = [] let currentVCs = super.viewControllers for vc in viewControllers { @@ -181,7 +180,7 @@ class XYNavigationController: UINavigationController { super.setViewControllers(warpedVCs, animated: animated) } - open override var viewControllers: [UIViewController]{ + public override var viewControllers: [UIViewController]{ set{ var warpedVCs: [UIViewController] = [] let currentVCs = super.viewControllers @@ -202,7 +201,7 @@ class XYNavigationController: UINavigationController { } // MARK: - visibleViewController/topViewController - open override var visibleViewController: UIViewController?{ + public override var visibleViewController: UIViewController?{ let visibelVC = super.visibleViewController if let contentVC = visibelVC as? XYContentController{ return contentVC.contentVc @@ -211,26 +210,66 @@ class XYNavigationController: UINavigationController { } } - open override var preferredStatusBarStyle: UIStatusBarStyle { + public override var preferredStatusBarStyle: UIStatusBarStyle { if let vc = topViewController { return vc.preferredStatusBarStyle } return .default } + + /// XYNav 自定义返回 pop 事件 + /// - Returns: 返回最顶部的 viewController + @objc func popByDefaultAction() -> UIViewController? { + return self.popViewController(animated: true) + } } +// MARK: - 拓展一些用户可全局调整的方法 extension XYNavigationController { - static public func setDefaultBackImage(_ image: UIImage) { + static var showClassNameInNavBar: Bool = false + static var navBarDefaultColor: UIColor? + + /// 设置全局返回按钮图标, 默认是 左箭头 < + /// - Parameter image: 自定义图片 + @objc static public func setDefaultBackImage(_ image: UIImage) { let image = image.withRenderingMode(.alwaysOriginal) backImage = image } - /// XYNav 自定义返回 pop 事件 - /// - Returns: 返回最顶部的 viewController - @objc func popByDefaultAction() -> UIViewController? { - return self.popViewController(animated: true) + /// 是否在导航栏展示当前 VC 的类名称 + /// - Parameter show: 默认不展示 + /// - Note: 这是一个 DEBUG 环境下特性, 即使设置为展示, 也只在 DEBUG 环境下生效 + @objc static public func showClassNameByDefault(_ show: Bool = false) { + showClassNameInNavBar = show + } + + /// 设置全局的导航栏颜色 + /// - Parameter barColor: 导航栏本身颜色 + /// - Note: 此属性设置之后会影响导航栏透明效果, 导航栏会变味不透明 + @objc static public func setDefaultBarColor(_ barColor: UIColor) { + navBarDefaultColor = barColor + } + + /// 对全局导航栏设置几个全局默认效果 + /// - Parameters: + /// - backImage: 导航栏返回按钮图标, 默认是 < + /// - showClassNameInNavbar: 是否在导航栏展示当前类的名称 + /// - navBarTintColor: 导航栏的全局背景色 + @objc static public func nav_setGlobal(backBtnImage: UIImage? = nil, + showClassNameInNavbar: Bool = false, + navBarTintColor: UIColor? = nil) { + if let backBtnImage = backBtnImage { + backImage = backBtnImage + } + + showClassNameByDefault(showClassNameInNavbar) + + if let navBarTintColor = navBarTintColor { + setDefaultBarColor(navBarTintColor) + } } + } extension XYNavigationController : UIGestureRecognizerDelegate{ @@ -251,15 +290,6 @@ extension XYNavigationController : UIGestureRecognizerDelegate{ } } -func getImageWithColor(_ color: UIColor) -> UIImage { - UIGraphicsBeginImageContextWithOptions(CGSize(width: 1, height: 1), false, 0) - let ctx = UIGraphicsGetCurrentContext() - ctx?.setFillColor(color.cgColor) - ctx?.fill(CGRect(x: 0, y: 0, width: 1, height: 1)) - let image = UIGraphicsGetImageFromCurrentImageContext() - return image ?? UIImage() -} - extension XYNavigationController { public override var tabBarItem: UITabBarItem! {