From a75afbe4c1aa6e9c540417b46ea1635bccac5d5b Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Thu, 6 Nov 2025 16:57:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BAnav=E5=AE=8F=20=E5=8E=BB?= =?UTF-8?q?=E9=99=A4tabbar=E9=80=8F=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- keyBoard/Class/Base/VC/BaseTabBarController.m | 14 ++++++ keyBoard/Class/Home/VC/HomeHotVC.m | 6 ++- keyBoard/KeyBoardPrefixHeader.pch | 43 +++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/keyBoard/Class/Base/VC/BaseTabBarController.m b/keyBoard/Class/Base/VC/BaseTabBarController.m index b5a0105..daabefe 100644 --- a/keyBoard/Class/Base/VC/BaseTabBarController.m +++ b/keyBoard/Class/Base/VC/BaseTabBarController.m @@ -19,6 +19,8 @@ - (void)viewDidLoad { [super viewDidLoad]; + + [self setupTabbarAppearance]; // Setup two tabs: Home & My, each embedded in BaseNavigationController HomeMainVC *home = [[HomeMainVC alloc] init]; home.title = @"首页"; @@ -36,6 +38,18 @@ /*[[KBAuthManager shared] saveAccessToken:@"TEST" refreshToken:nil expiryDate:[NSDate dateWithTimeIntervalSinceNow:3600] userIdentifier:nil]*/; } +- (void)setupTabbarAppearance{ + // 让 TabBar 不透明,子控制器 view 不再延伸到 TabBar 下 + self.tabBar.translucent = NO; + if (@available(iOS 15.0, *)) { + UITabBarAppearance *a = [UITabBarAppearance new]; + [a configureWithOpaqueBackground]; + a.backgroundColor = [UIColor whiteColor]; + self.tabBar.standardAppearance = a; + self.tabBar.scrollEdgeAppearance = a; + } +} + /* #pragma mark - Navigation diff --git a/keyBoard/Class/Home/VC/HomeHotVC.m b/keyBoard/Class/Home/VC/HomeHotVC.m index 602e7b0..402ec42 100644 --- a/keyBoard/Class/Home/VC/HomeHotVC.m +++ b/keyBoard/Class/Home/VC/HomeHotVC.m @@ -83,7 +83,11 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; HomeRankVC *vc = [[HomeRankVC alloc] init]; - [self.navigationController pushViewController:vc animated:true]; +// [self.navigationController pushViewController:vc animated:true]; + UINavigationController *nav = KB_CURRENT_NAV; + [nav pushViewController:vc animated:true]; + NSLog(@"==="); + } #pragma mark - Lazy diff --git a/keyBoard/KeyBoardPrefixHeader.pch b/keyBoard/KeyBoardPrefixHeader.pch index 3a9d4b2..6f7a6ad 100644 --- a/keyBoard/KeyBoardPrefixHeader.pch +++ b/keyBoard/KeyBoardPrefixHeader.pch @@ -129,4 +129,47 @@ static inline BOOL KBDeviceHasNotchRuntime(void) { #define KB_DEVICE_HAS_NOTCH (KBDeviceHasNotchRuntime()) #endif +// --- 获取当前可用的 UINavigationController --- +// 说明: +// 1. 优先取最顶层可见 VC(见 UIViewController+Extension),再解析其所在的导航容器; +// 2. 兼容被 UITabBarController 包裹、直接为 UINavigationController、或普通 VC 嵌套等场景; +// 3. 若无法获取,返回 nil。 +static inline __kindof UINavigationController *KB_CurrentNavigationController(void) { + UIViewController *top = [UIViewController kb_topMostViewController]; + if (!top) { + UIWindow *w = KB_KeyWindow(); + top = w.rootViewController; + } + if (!top) return nil; + + // 直接就是导航控制器 + if ([top isKindOfClass:[UINavigationController class]]) { + return (UINavigationController *)top; + } + // 顶层是 TabBar,取选中的子控制器 + if ([top isKindOfClass:[UITabBarController class]]) { + UITabBarController *tab = (UITabBarController *)top; + UIViewController *sel = tab.selectedViewController ?: tab.viewControllers.firstObject; + if ([sel isKindOfClass:[UINavigationController class]]) return (UINavigationController *)sel; + return sel.navigationController ?: nil; + } + // 普通 VC,取其所属导航 + if (top.navigationController) return top.navigationController; + + // 兜底:尝试从 root 解析 + UIViewController *root = KB_KeyWindow().rootViewController; + if ([root isKindOfClass:[UINavigationController class]]) return (UINavigationController *)root; + if ([root isKindOfClass:[UITabBarController class]]) { + UIViewController *sel = ((UITabBarController *)root).selectedViewController; + if ([sel isKindOfClass:[UINavigationController class]]) return (UINavigationController *)sel; + return sel.navigationController ?: nil; + } + return nil; +} + +// 简便宏:直接当做属性来用,例如:`[KB_CURRENT_NAV pushViewController:vc animated:YES];` +#ifndef KB_CURRENT_NAV +#define KB_CURRENT_NAV (KB_CurrentNavigationController()) +#endif + #endif /* KeyBoardPrefixHeader_pch */