2025-10-29 14:17:26 +08:00
|
|
|
|
//
|
|
|
|
|
|
// BaseTabBarController.m
|
|
|
|
|
|
// keyBoard
|
|
|
|
|
|
//
|
|
|
|
|
|
// Created by Mac on 2025/10/29.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#import "BaseTabBarController.h"
|
2025-11-06 16:05:28 +08:00
|
|
|
|
#import "HomeMainVC.h"
|
2026-01-16 13:38:03 +08:00
|
|
|
|
#import "KBAiMainVC.h"
|
2026-01-16 21:37:18 +08:00
|
|
|
|
#import "KBShopVC.h"
|
|
|
|
|
|
#import "MyVC.h"
|
2026-01-26 16:53:41 +08:00
|
|
|
|
#import "KBAIHomeVC.h"
|
2026-01-16 13:38:03 +08:00
|
|
|
|
|
2026-01-16 21:37:18 +08:00
|
|
|
|
// #import "KBCommunityVC.h"
|
2025-11-06 19:19:12 +08:00
|
|
|
|
|
2025-10-29 14:28:57 +08:00
|
|
|
|
#import "BaseNavigationController.h"
|
2025-10-31 16:50:15 +08:00
|
|
|
|
#import "KBAuthManager.h"
|
2026-01-19 19:14:20 +08:00
|
|
|
|
#import "KBCustomTabBar.h"
|
|
|
|
|
|
#import "Masonry.h"
|
|
|
|
|
|
@interface BaseTabBarController () <KBCustomTabBarDelegate>
|
2025-10-29 14:17:26 +08:00
|
|
|
|
|
2026-01-19 19:14:20 +08:00
|
|
|
|
/// 自定义 TabBar
|
|
|
|
|
|
@property(nonatomic, strong) KBCustomTabBar *customTabBar;
|
2026-01-16 21:37:18 +08:00
|
|
|
|
|
2025-10-29 14:17:26 +08:00
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation BaseTabBarController
|
|
|
|
|
|
|
|
|
|
|
|
- (void)viewDidLoad {
|
2026-01-16 21:37:18 +08:00
|
|
|
|
[super viewDidLoad];
|
2025-11-06 16:57:28 +08:00
|
|
|
|
|
2026-01-16 21:37:18 +08:00
|
|
|
|
self.delegate = self;
|
2025-12-03 15:19:03 +08:00
|
|
|
|
|
2026-01-16 21:37:18 +08:00
|
|
|
|
// 组装 4 个 Tab:首页 / 商城 / 社区 / 我的
|
|
|
|
|
|
// 图标位于 Assets.xcassets/Tabbar 下:tab_home/tab_home_selected 等
|
2025-11-06 19:29:52 +08:00
|
|
|
|
|
2026-01-16 21:37:18 +08:00
|
|
|
|
// 首页
|
|
|
|
|
|
HomeMainVC *home = [[HomeMainVC alloc] init];
|
|
|
|
|
|
BaseNavigationController *navHome =
|
|
|
|
|
|
[[BaseNavigationController alloc] initWithRootViewController:home];
|
|
|
|
|
|
navHome.tabBarItem = [self tabItemWithTitle:KBLocalized(@"Home")
|
2025-11-06 19:29:52 +08:00
|
|
|
|
image:@"tab_home"
|
|
|
|
|
|
selectedImg:@"tab_home_selected"];
|
|
|
|
|
|
|
2026-01-16 21:37:18 +08:00
|
|
|
|
// 商城
|
|
|
|
|
|
KBShopVC *shop = [[KBShopVC alloc] init];
|
|
|
|
|
|
BaseNavigationController *navShop =
|
|
|
|
|
|
[[BaseNavigationController alloc] initWithRootViewController:shop];
|
|
|
|
|
|
navShop.tabBarItem = [self tabItemWithTitle:KBLocalized(@"Shop")
|
2025-11-06 19:29:52 +08:00
|
|
|
|
image:@"tab_shop"
|
|
|
|
|
|
selectedImg:@"tab_shop_selected"];
|
2025-10-29 14:28:57 +08:00
|
|
|
|
|
2026-01-16 21:37:18 +08:00
|
|
|
|
// AI
|
2026-01-26 16:53:41 +08:00
|
|
|
|
KBAIHomeVC *aiMainVC = [[KBAIHomeVC alloc] init];
|
2026-01-16 21:37:18 +08:00
|
|
|
|
// community.title = KBLocalized(@"Circle");
|
|
|
|
|
|
BaseNavigationController *navCommunity =
|
|
|
|
|
|
[[BaseNavigationController alloc] initWithRootViewController:aiMainVC];
|
|
|
|
|
|
navCommunity.tabBarItem = [self tabItemWithTitle:KBLocalized(@"Circle")
|
2026-01-16 13:38:03 +08:00
|
|
|
|
image:@"tab_shequ"
|
|
|
|
|
|
selectedImg:@"tab_shequ_selected"];
|
2025-11-06 19:29:52 +08:00
|
|
|
|
|
2026-01-16 21:37:18 +08:00
|
|
|
|
// 我的
|
|
|
|
|
|
MyVC *my = [[MyVC alloc] init];
|
|
|
|
|
|
BaseNavigationController *navMy =
|
|
|
|
|
|
[[BaseNavigationController alloc] initWithRootViewController:my];
|
|
|
|
|
|
navMy.tabBarItem = [self tabItemWithTitle:KBLocalized(@"Mine")
|
|
|
|
|
|
image:@"tab_my"
|
|
|
|
|
|
selectedImg:@"tab_my_selected"];
|
|
|
|
|
|
|
2026-01-19 19:14:20 +08:00
|
|
|
|
self.viewControllers = @[ navHome, navShop, navCommunity, navMy ];
|
|
|
|
|
|
|
|
|
|
|
|
// 不隐藏系统 TabBar,而是将自定义视图添加到 TabBar 上
|
|
|
|
|
|
[self setupCustomTabBarOnNativeTabBar];
|
2026-01-16 21:37:18 +08:00
|
|
|
|
|
|
|
|
|
|
// 测试储存Token
|
|
|
|
|
|
// [[KBAuthManager shared] saveAccessToken:@"TEST" refreshToken:nil
|
|
|
|
|
|
// expiryDate:[NSDate dateWithTimeIntervalSinceNow:3600]
|
|
|
|
|
|
// userIdentifier:nil];
|
|
|
|
|
|
// [[KBAuthManager shared] signOut];
|
2025-10-29 14:17:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-19 19:14:20 +08:00
|
|
|
|
- (void)setupCustomTabBarOnNativeTabBar {
|
|
|
|
|
|
// 1. 持续隐藏原生 TabBar 的所有按钮
|
|
|
|
|
|
[self hideNativeTabBarButtons];
|
|
|
|
|
|
|
|
|
|
|
|
// 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;
|
2026-01-16 21:37:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-06 16:57:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-19 19:14:20 +08:00
|
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
|
|
|
|
|
|
|
|
// 每次视图即将出现时,确保原生按钮被隐藏
|
|
|
|
|
|
[self hideNativeTabBarButtons];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-06 19:29:52 +08:00
|
|
|
|
// 统一构造 TabBarItem,原图渲染,避免被系统 Tint 着色
|
2026-01-16 21:37:18 +08:00
|
|
|
|
- (UITabBarItem *)tabItemWithTitle:(NSString *)title
|
|
|
|
|
|
image:(NSString *)imageName
|
|
|
|
|
|
selectedImg:(NSString *)selectedName {
|
|
|
|
|
|
UIImage *img = [[UIImage imageNamed:imageName]
|
|
|
|
|
|
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
|
|
|
|
|
|
UIImage *sel = [[UIImage imageNamed:selectedName]
|
|
|
|
|
|
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
|
|
|
|
|
|
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:title
|
|
|
|
|
|
image:img
|
|
|
|
|
|
selectedImage:sel];
|
|
|
|
|
|
return item;
|
2025-11-06 19:29:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-19 19:14:20 +08:00
|
|
|
|
#pragma mark - KBCustomTabBarDelegate
|
2025-12-03 15:19:03 +08:00
|
|
|
|
|
2026-01-19 19:14:20 +08:00
|
|
|
|
- (void)customTabBar:(KBCustomTabBar *)tabBar
|
|
|
|
|
|
didSelectItemAtIndex:(NSInteger)index {
|
|
|
|
|
|
// 登录检查
|
2026-01-16 21:37:18 +08:00
|
|
|
|
if ((index == 1 || index == 2) && ![KBUserSessionManager shared].isLoggedIn) {
|
|
|
|
|
|
[[KBUserSessionManager shared] goLoginVC];
|
2026-01-19 19:14:20 +08:00
|
|
|
|
return;
|
2026-01-16 21:37:18 +08:00
|
|
|
|
}
|
2025-12-03 15:19:03 +08:00
|
|
|
|
|
2026-01-19 19:14:20 +08:00
|
|
|
|
// 切换 VC
|
|
|
|
|
|
self.selectedIndex = index;
|
2026-01-16 21:37:18 +08:00
|
|
|
|
|
2026-01-19 19:14:20 +08:00
|
|
|
|
// 设置 TabBar 背景(索引2是 KBAiMainVC)
|
2026-01-16 21:37:18 +08:00
|
|
|
|
if (index == 2) {
|
2026-01-19 19:14:20 +08:00
|
|
|
|
[self.customTabBar setTransparentBackground:YES];
|
2026-01-16 21:37:18 +08:00
|
|
|
|
} else {
|
2026-01-19 19:14:20 +08:00
|
|
|
|
[self.customTabBar setTransparentBackground:NO];
|
2026-01-16 21:37:18 +08:00
|
|
|
|
}
|
2025-12-03 15:19:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-29 14:17:26 +08:00
|
|
|
|
/*
|
|
|
|
|
|
#pragma mark - Navigation
|
|
|
|
|
|
|
2026-01-16 21:37:18 +08:00
|
|
|
|
// In a storyboard-based application, you will often want to do a little
|
|
|
|
|
|
preparation before navigation
|
2025-10-29 14:17:26 +08:00
|
|
|
|
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
|
|
|
|
|
|
// Get the new view controller using [segue destinationViewController].
|
|
|
|
|
|
// Pass the selected object to the new view controller.
|
|
|
|
|
|
}
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
@end
|