Files
keyboard/keyBoard/Class/Base/VC/BaseTabBarController.m

226 lines
8.2 KiB
Mathematica
Raw Normal View History

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"
#import "KBAiMainVC.h"
2026-01-16 21:37:18 +08:00
#import "KBShopVC.h"
#import "MyVC.h"
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"
#import "KBAuthManager.h"
2025-10-29 14:17:26 +08:00
@interface BaseTabBarController ()
2026-01-16 21:37:18 +08:00
/// TabBar appearance
@property(nonatomic, strong)
UITabBarAppearance *originalAppearance API_AVAILABLE(ios(13.0));
@property(nonatomic, strong)
UITabBarAppearance *transparentAppearance API_AVAILABLE(ios(13.0));
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
[self setupTabbarAppearance];
2026-01-16 21:37:18 +08:00
// 4 Tab / / /
// Assets.xcassets/Tabbar tab_home/tab_home_selected
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")
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")
image:@"tab_shop"
selectedImg:@"tab_shop_selected"];
2025-10-29 14:28:57 +08:00
2026-01-16 21:37:18 +08:00
// 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"];
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"];
self.viewControllers = @[ navHome, navShop, aiMainVC, navMy ];
// 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-16 21:37:18 +08:00
- (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];
}
2025-11-06 16:57:28 +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-12-03 15:19:03 +08:00
#pragma mark - UITabBarControllerDelegate
2026-01-16 21:37:18 +08:00
- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController {
// tab
if (tabBarController.selectedViewController == viewController) {
return YES;
}
2025-12-03 15:19:03 +08:00
2026-01-16 21:37:18 +08:00
NSUInteger index =
[tabBarController.viewControllers indexOfObject:viewController];
if (index == NSNotFound) {
return YES;
}
2025-12-03 15:19:03 +08:00
2026-01-16 21:37:18 +08:00
// tab 4 3
if ((index == 1 || index == 2) && ![KBUserSessionManager shared].isLoggedIn) {
[[KBUserSessionManager shared] goLoginVC];
return NO;
}
2025-12-03 15:19:03 +08:00
2026-01-16 21:37:18 +08:00
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController {
NSUInteger index =
[tabBarController.viewControllers indexOfObject:viewController];
// tab2 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];
}
}
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