创建nav宏
去除tabbar透明
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 */
|
||||
|
||||
Reference in New Issue
Block a user