创建nav宏
去除tabbar透明
This commit is contained in:
@@ -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