Files
keyboard/keyBoard/Class/Base/VC/BaseTabBarController.m
2026-01-16 21:37:18 +08:00

226 lines
8.2 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// BaseTabBarController.m
// keyBoard
//
// Created by Mac on 2025/10/29.
//
#import "BaseTabBarController.h"
#import "HomeMainVC.h"
#import "KBAiMainVC.h"
#import "KBShopVC.h"
#import "MyVC.h"
// #import "KBCommunityVC.h"
#import "BaseNavigationController.h"
#import "KBAuthManager.h"
@interface BaseTabBarController ()
/// 原始的 TabBar appearance用于恢复
@property(nonatomic, strong)
UITabBarAppearance *originalAppearance API_AVAILABLE(ios(13.0));
@property(nonatomic, strong)
UITabBarAppearance *transparentAppearance API_AVAILABLE(ios(13.0));
@end
@implementation BaseTabBarController
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
[self setupTabbarAppearance];
// 组装 4 个 Tab首页 / 商城 / 社区 / 我的
// 图标位于 Assets.xcassets/Tabbar 下tab_home/tab_home_selected 等
// 首页
HomeMainVC *home = [[HomeMainVC alloc] init];
BaseNavigationController *navHome =
[[BaseNavigationController alloc] initWithRootViewController:home];
navHome.tabBarItem = [self tabItemWithTitle:KBLocalized(@"Home")
image:@"tab_home"
selectedImg:@"tab_home_selected"];
// 商城
KBShopVC *shop = [[KBShopVC alloc] init];
BaseNavigationController *navShop =
[[BaseNavigationController alloc] initWithRootViewController:shop];
navShop.tabBarItem = [self tabItemWithTitle:KBLocalized(@"Shop")
image:@"tab_shop"
selectedImg:@"tab_shop_selected"];
// AI
KBAiMainVC *aiMainVC = [[KBAiMainVC alloc] init];
// community.title = KBLocalized(@"Circle");
BaseNavigationController *navCommunity =
[[BaseNavigationController alloc] initWithRootViewController:aiMainVC];
navCommunity.tabBarItem = [self tabItemWithTitle:KBLocalized(@"Circle")
image:@"tab_shequ"
selectedImg:@"tab_shequ_selected"];
// 我的
MyVC *my = [[MyVC alloc] init];
BaseNavigationController *navMy =
[[BaseNavigationController alloc] initWithRootViewController:my];
navMy.tabBarItem = [self tabItemWithTitle:KBLocalized(@"Mine")
image:@"tab_my"
selectedImg:@"tab_my_selected"];
self.viewControllers = @[ navHome, navShop, aiMainVC, navMy ];
// 测试储存Token
// [[KBAuthManager shared] saveAccessToken:@"TEST" refreshToken:nil
// expiryDate:[NSDate dateWithTimeIntervalSinceNow:3600]
// userIdentifier:nil];
// [[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;
// 保存原始 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];
}
}
// 统一构造 TabBarItem原图渲染避免被系统 Tint 着色
- (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;
}
#pragma mark - UITabBarControllerDelegate
- (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未登录时先跳转登录页
if ((index == 1 || index == 2) && ![KBUserSessionManager shared].isLoggedIn) {
[[KBUserSessionManager shared] goLoginVC];
return NO;
}
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController {
NSUInteger index =
[tabBarController.viewControllers indexOfObject:viewController];
// 第三个 tab索引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];
}
} 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];
}
}
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little
preparation before navigation
- (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