处理tabbar
This commit is contained in:
@@ -15,13 +15,12 @@
|
||||
|
||||
#import "BaseNavigationController.h"
|
||||
#import "KBAuthManager.h"
|
||||
@interface BaseTabBarController ()
|
||||
#import "KBCustomTabBar.h"
|
||||
#import "Masonry.h"
|
||||
@interface BaseTabBarController () <KBCustomTabBarDelegate>
|
||||
|
||||
/// 原始的 TabBar appearance(用于恢复)
|
||||
@property(nonatomic, strong)
|
||||
UITabBarAppearance *originalAppearance API_AVAILABLE(ios(13.0));
|
||||
@property(nonatomic, strong)
|
||||
UITabBarAppearance *transparentAppearance API_AVAILABLE(ios(13.0));
|
||||
/// 自定义 TabBar
|
||||
@property(nonatomic, strong) KBCustomTabBar *customTabBar;
|
||||
|
||||
@end
|
||||
|
||||
@@ -32,8 +31,6 @@
|
||||
|
||||
self.delegate = self;
|
||||
|
||||
[self setupTabbarAppearance];
|
||||
|
||||
// 组装 4 个 Tab:首页 / 商城 / 社区 / 我的
|
||||
// 图标位于 Assets.xcassets/Tabbar 下:tab_home/tab_home_selected 等
|
||||
|
||||
@@ -70,7 +67,10 @@
|
||||
image:@"tab_my"
|
||||
selectedImg:@"tab_my_selected"];
|
||||
|
||||
self.viewControllers = @[ navHome, navShop, aiMainVC, navMy ];
|
||||
self.viewControllers = @[ navHome, navShop, navCommunity, navMy ];
|
||||
|
||||
// 不隐藏系统 TabBar,而是将自定义视图添加到 TabBar 上
|
||||
[self setupCustomTabBarOnNativeTabBar];
|
||||
|
||||
// 测试储存Token
|
||||
// [[KBAuthManager shared] saveAccessToken:@"TEST" refreshToken:nil
|
||||
@@ -79,66 +79,60 @@
|
||||
// [[KBAuthManager shared] signOut];
|
||||
}
|
||||
|
||||
- (void)setupTabbarAppearance {
|
||||
// 让 TabBar 不透明,子控制器 view 不再延伸到 TabBar 下
|
||||
self.tabBar.translucent = NO;
|
||||
if (@available(iOS 15.0, *)) {
|
||||
UITabBarAppearance *a = [UITabBarAppearance new];
|
||||
[a configureWithOpaqueBackground];
|
||||
a.backgroundColor = [UIColor whiteColor];
|
||||
// 设置选中标题为黑色(iOS 15+ 需通过 appearance)
|
||||
NSDictionary *selAttr =
|
||||
@{NSForegroundColorAttributeName : [UIColor blackColor]};
|
||||
a.stackedLayoutAppearance.selected.titleTextAttributes = selAttr;
|
||||
a.inlineLayoutAppearance.selected.titleTextAttributes = selAttr;
|
||||
a.compactInlineLayoutAppearance.selected.titleTextAttributes = selAttr;
|
||||
self.tabBar.standardAppearance = a;
|
||||
self.tabBar.scrollEdgeAppearance = a;
|
||||
- (void)setupCustomTabBarOnNativeTabBar {
|
||||
// 1. 持续隐藏原生 TabBar 的所有按钮
|
||||
[self hideNativeTabBarButtons];
|
||||
|
||||
// 保存原始 appearance
|
||||
self.originalAppearance = a;
|
||||
|
||||
// 创建透明 appearance
|
||||
UITabBarAppearance *transparentA = [UITabBarAppearance new];
|
||||
[transparentA configureWithTransparentBackground];
|
||||
transparentA.backgroundColor = [UIColor clearColor];
|
||||
transparentA.stackedLayoutAppearance.selected.titleTextAttributes = selAttr;
|
||||
transparentA.inlineLayoutAppearance.selected.titleTextAttributes = selAttr;
|
||||
transparentA.compactInlineLayoutAppearance.selected.titleTextAttributes =
|
||||
selAttr;
|
||||
self.transparentAppearance = transparentA;
|
||||
} else if (@available(iOS 13.0, *)) {
|
||||
UITabBarAppearance *a = [UITabBarAppearance new];
|
||||
[a configureWithOpaqueBackground];
|
||||
a.backgroundColor = [UIColor whiteColor];
|
||||
NSDictionary *selAttr =
|
||||
@{NSForegroundColorAttributeName : [UIColor blackColor]};
|
||||
a.stackedLayoutAppearance.selected.titleTextAttributes = selAttr;
|
||||
a.inlineLayoutAppearance.selected.titleTextAttributes = selAttr;
|
||||
a.compactInlineLayoutAppearance.selected.titleTextAttributes = selAttr;
|
||||
self.tabBar.standardAppearance = a;
|
||||
self.tabBar.tintColor = [UIColor blackColor];
|
||||
|
||||
// 保存原始 appearance
|
||||
self.originalAppearance = a;
|
||||
|
||||
// 创建透明 appearance
|
||||
UITabBarAppearance *transparentA = [UITabBarAppearance new];
|
||||
[transparentA configureWithTransparentBackground];
|
||||
transparentA.backgroundColor = [UIColor clearColor];
|
||||
transparentA.stackedLayoutAppearance.selected.titleTextAttributes = selAttr;
|
||||
transparentA.inlineLayoutAppearance.selected.titleTextAttributes = selAttr;
|
||||
transparentA.compactInlineLayoutAppearance.selected.titleTextAttributes =
|
||||
selAttr;
|
||||
self.transparentAppearance = transparentA;
|
||||
} else {
|
||||
// 老系统用 tintColor/appearance 做回退
|
||||
self.tabBar.tintColor = [UIColor blackColor];
|
||||
[[UITabBarItem appearance] setTitleTextAttributes:@{
|
||||
NSForegroundColorAttributeName : [UIColor blackColor]
|
||||
}
|
||||
forState:UIControlStateSelected];
|
||||
// 2. 创建自定义 TabBar
|
||||
NSMutableArray *items = [NSMutableArray array];
|
||||
for (UIViewController *vc in self.viewControllers) {
|
||||
[items addObject:vc.tabBarItem];
|
||||
}
|
||||
|
||||
self.customTabBar = [[KBCustomTabBar alloc] initWithItems:items];
|
||||
self.customTabBar.delegate = self;
|
||||
|
||||
// 3. 设置 frame 覆盖整个 TabBar 区域
|
||||
self.customTabBar.frame = self.tabBar.bounds;
|
||||
|
||||
// 4. 使用 autoresizingMask 自动适配尺寸变化
|
||||
self.customTabBar.autoresizingMask =
|
||||
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
|
||||
|
||||
// 5. 将自定义 TabBar 添加到原生 TabBar 上
|
||||
[self.tabBar addSubview:self.customTabBar];
|
||||
|
||||
// 6. 禁用原生 TabBar 的交互,只让自定义视图响应
|
||||
self.tabBar.userInteractionEnabled = YES;
|
||||
for (UIView *subview in self.tabBar.subviews) {
|
||||
if (subview != self.customTabBar &&
|
||||
![NSStringFromClass([subview class]) containsString:@"Background"]) {
|
||||
subview.userInteractionEnabled = NO;
|
||||
subview.hidden = YES;
|
||||
}
|
||||
}
|
||||
|
||||
// 7. 设置初始背景色(默认选中索引0,白色背景)
|
||||
[self.customTabBar setTransparentBackground:NO];
|
||||
}
|
||||
|
||||
- (void)hideNativeTabBarButtons {
|
||||
// 移除或隐藏原生 TabBar 的所有子视图(按钮等)
|
||||
for (UIView *subview in self.tabBar.subviews) {
|
||||
// 保留系统的 _UIBarBackground 背景视图,隐藏其他视图
|
||||
if (![NSStringFromClass([subview class]) containsString:@"Background"] &&
|
||||
subview != self.customTabBar) {
|
||||
subview.hidden = YES;
|
||||
subview.alpha = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)viewWillAppear:(BOOL)animated {
|
||||
[super viewWillAppear:animated];
|
||||
|
||||
// 每次视图即将出现时,确保原生按钮被隐藏
|
||||
[self hideNativeTabBarButtons];
|
||||
}
|
||||
|
||||
// 统一构造 TabBarItem,原图渲染,避免被系统 Tint 着色
|
||||
@@ -155,59 +149,24 @@
|
||||
return item;
|
||||
}
|
||||
|
||||
#pragma mark - UITabBarControllerDelegate
|
||||
#pragma mark - KBCustomTabBarDelegate
|
||||
|
||||
- (BOOL)tabBarController:(UITabBarController *)tabBarController
|
||||
shouldSelectViewController:(UIViewController *)viewController {
|
||||
// 已经是当前选中的 tab,直接允许(避免重复拦截)
|
||||
if (tabBarController.selectedViewController == viewController) {
|
||||
return YES;
|
||||
}
|
||||
|
||||
NSUInteger index =
|
||||
[tabBarController.viewControllers indexOfObject:viewController];
|
||||
if (index == NSNotFound) {
|
||||
return YES;
|
||||
}
|
||||
|
||||
// “我的” tab 默认为第 4 个(索引 3),未登录时先跳转登录页
|
||||
- (void)customTabBar:(KBCustomTabBar *)tabBar
|
||||
didSelectItemAtIndex:(NSInteger)index {
|
||||
// 登录检查
|
||||
if ((index == 1 || index == 2) && ![KBUserSessionManager shared].isLoggedIn) {
|
||||
[[KBUserSessionManager shared] goLoginVC];
|
||||
return NO;
|
||||
return;
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
// 切换 VC
|
||||
self.selectedIndex = index;
|
||||
|
||||
- (void)tabBarController:(UITabBarController *)tabBarController
|
||||
didSelectViewController:(UIViewController *)viewController {
|
||||
NSUInteger index =
|
||||
[tabBarController.viewControllers indexOfObject:viewController];
|
||||
|
||||
// 第三个 tab(索引2)是 KBAiMainVC,背景透明
|
||||
// 设置 TabBar 背景(索引2是 KBAiMainVC)
|
||||
if (index == 2) {
|
||||
if (@available(iOS 13.0, *)) {
|
||||
self.tabBar.standardAppearance = self.transparentAppearance;
|
||||
if (@available(iOS 15.0, *)) {
|
||||
self.tabBar.scrollEdgeAppearance = self.transparentAppearance;
|
||||
}
|
||||
} else {
|
||||
// iOS 13 以下,直接设置背景色
|
||||
self.tabBar.barTintColor = [UIColor clearColor];
|
||||
self.tabBar.backgroundColor = [UIColor clearColor];
|
||||
}
|
||||
[self.customTabBar setTransparentBackground:YES];
|
||||
} else {
|
||||
// 其他 tab,恢复原始背景
|
||||
if (@available(iOS 13.0, *)) {
|
||||
self.tabBar.standardAppearance = self.originalAppearance;
|
||||
if (@available(iOS 15.0, *)) {
|
||||
self.tabBar.scrollEdgeAppearance = self.originalAppearance;
|
||||
}
|
||||
} else {
|
||||
// iOS 13 以下,恢复白色背景
|
||||
self.tabBar.barTintColor = [UIColor whiteColor];
|
||||
self.tabBar.backgroundColor = [UIColor whiteColor];
|
||||
}
|
||||
[self.customTabBar setTransparentBackground:NO];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user