123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- //
- // KSTabBarViewController.m
- // KulexiuForTeacher
- //
- // Created by Kyle on 2022/3/17.
- //
- #import "KSTabBarViewController.h"
- #import "CustomNavViewController.h"
- #import "KSBaseViewController.h"
- #import "UIImage+Color.h"
- #import "AnimationHelper.h"
- @interface KSTabBarViewController ()<UITabBarControllerDelegate>
- @end
- @implementation KSTabBarViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- self.delegate = self;
- [self configItems];
- }
- - (void)configItems {
- NSArray *controllerArray = @[@"HomeViewController",@"CourseViewController",@"ChatViewController",@"ShopMallViewController",@"MineViewController"];
- NSArray *titleArray = @[@"首页",@"课表",@"聊天",@"商城",@"我的"];
- NSArray *imageArray = @[@"tab_home_unselected",@"tab_course_unselected",@"tab_chat_unselected",@"tab_mall_unselected",@"tab_mine_unselected"];
- NSArray *selectedImgArray = @[@"tab_home_selected",@"tab_course_selected",@"tab_chat_selected",@"tab_mall_selected",@"tab_mine_selected"];
- NSMutableArray *ctrlArray = [NSMutableArray array];
- for (NSInteger i = 0; i < controllerArray.count; i++) {
- CustomNavViewController *navCtrl = [self createNavControllerWithClassName:controllerArray[i] title:titleArray[i] imageName:imageArray[i] selectedImageName:selectedImgArray[i]];
- [ctrlArray addObject:navCtrl];
- }
- self.viewControllers = [NSArray arrayWithArray:ctrlArray];
- if (@available(iOS 13.0, *)) {
- [[UITabBar appearance] setUnselectedItemTintColor:HexRGB(0x808492)];
- [[UITabBar appearance] setTintColor:HexRGB(0x283240)];
- } else {
- [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x808492),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateNormal];
- [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x283240),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateSelected];
- }
- if (@available(iOS 15.0, *)) {
- // 适配Xcode 13 iOS 15tabbar透明的问题
- UITabBarAppearance * appearance = [[UITabBarAppearance alloc] init];
- [appearance configureWithOpaqueBackground];
- appearance.backgroundColor = UIColor.whiteColor;
- appearance.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
- [UITabBar appearance].scrollEdgeAppearance = appearance;
- [UITabBar appearance].standardAppearance = appearance;
- }
- [UITabBar appearance].backgroundColor = UIColor.whiteColor;
- }
- - (void)removeControllerNotifer {
- NSArray *controllerArray = self.viewControllers;
- for (CustomNavViewController *navCtrl in controllerArray) {
- UIViewController *ctrl = navCtrl.viewControllers[0];
- if ([ctrl isKindOfClass:[KSBaseViewController class]]) {
- KSBaseViewController *baseCtrl = (KSBaseViewController *)ctrl;
- [baseCtrl removeNotifier];
- }
- }
- }
- - (CustomNavViewController *)createNavControllerWithClassName:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
- KSBaseViewController *controller = [[NSClassFromString(className) alloc] init];
- controller.title = title;
- controller.tabBarItem.title = title;
- controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
- controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
- [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x808492),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateNormal];
- [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x283240),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateSelected];
- CustomNavViewController *navCtrl= [[CustomNavViewController alloc] initWithRootViewController:controller];
- return navCtrl;
- }
- - (void)tabBarSelectedWithIndex:(NSInteger)index {
- [self setSelectedIndex:index];
- }
- - (void)noteNewsWithIndex:(NSInteger)index count:(NSInteger)count { //提醒
- UITabBarItem * tabBarItem = self.tabBar.items[index];
- if (count == 0) {
- [tabBarItem setBadgeValue:nil];// 隐藏角标
- }else if (count > 99) {
- [tabBarItem setBadgeValue:@"99+"];
-
- } else {
- [tabBarItem setBadgeValue:[NSString stringWithFormat:@"%zd",count]];
- }
- }
- - (void)clearNewsWithIndex:(NSInteger)index { //清除
- UITabBarItem * tabBarItem = self.tabBar.items[index];
- [tabBarItem setBadgeValue:nil];
- }
- #pragma mark ----- UITabBarControllerDelegate
- - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
- [self setAnaimationWithTabBarController:tabBarController selectViewController:viewController];
- }
- - (void)setAnaimationWithTabBarController:(UITabBarController *)tabBarController selectViewController:(UIViewController *)viewController {
- //1.
- NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
- __block NSMutableArray <UIView *>*tabBarSwappableImageViews = [NSMutableArray arrayWithCapacity:4];
- //2.
- for (UIView *tempView in tabBarController.tabBar.subviews) {
- if ([tempView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
- for (UIImageView *tempImageView in tempView.subviews) {
- if ([tempImageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
- [tabBarSwappableImageViews addObject:tempImageView];
- }
- }
- }
- }
- //3.
- __block UIView *currentTabBarSwappableImageView = tabBarSwappableImageViews[index];
- [AnimationHelper lottieAnimation:currentTabBarSwappableImageView index:index];
- }
- /*
- #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
|