KSTabBarViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // KSTabBarViewController.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by Kyle on 2022/3/17.
  6. //
  7. #import "KSTabBarViewController.h"
  8. #import "CustomNavViewController.h"
  9. #import "KSBaseViewController.h"
  10. #import "UIImage+Color.h"
  11. #import "AnimationHelper.h"
  12. @interface KSTabBarViewController ()<UITabBarControllerDelegate>
  13. @end
  14. @implementation KSTabBarViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. // Do any additional setup after loading the view.
  18. self.delegate = self;
  19. [self configItems];
  20. }
  21. - (void)configItems {
  22. NSArray *controllerArray = @[@"HomeViewController",@"CourseViewController",@"ChatViewController",@"ShopMallViewController",@"MineViewController"];
  23. NSArray *titleArray = @[@"首页",@"课表",@"聊天",@"商城",@"我的"];
  24. NSArray *imageArray = @[@"tab_home_unselected",@"tab_course_unselected",@"tab_chat_unselected",@"tab_mall_unselected",@"tab_mine_unselected"];
  25. NSArray *selectedImgArray = @[@"tab_home_selected",@"tab_course_selected",@"tab_chat_selected",@"tab_mall_selected",@"tab_mine_selected"];
  26. NSMutableArray *ctrlArray = [NSMutableArray array];
  27. for (NSInteger i = 0; i < controllerArray.count; i++) {
  28. CustomNavViewController *navCtrl = [self createNavControllerWithClassName:controllerArray[i] title:titleArray[i] imageName:imageArray[i] selectedImageName:selectedImgArray[i]];
  29. [ctrlArray addObject:navCtrl];
  30. }
  31. self.viewControllers = [NSArray arrayWithArray:ctrlArray];
  32. if (@available(iOS 13.0, *)) {
  33. [[UITabBar appearance] setUnselectedItemTintColor:HexRGB(0x808492)];
  34. [[UITabBar appearance] setTintColor:HexRGB(0x283240)];
  35. } else {
  36. [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x808492),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateNormal];
  37. [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x283240),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateSelected];
  38. }
  39. if (@available(iOS 15.0, *)) {
  40. // 适配Xcode 13 iOS 15tabbar透明的问题
  41. UITabBarAppearance * appearance = [[UITabBarAppearance alloc] init];
  42. [appearance configureWithOpaqueBackground];
  43. appearance.backgroundColor = UIColor.whiteColor;
  44. appearance.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
  45. [UITabBar appearance].scrollEdgeAppearance = appearance;
  46. [UITabBar appearance].standardAppearance = appearance;
  47. }
  48. [UITabBar appearance].backgroundColor = UIColor.whiteColor;
  49. }
  50. - (void)removeControllerNotifer {
  51. NSArray *controllerArray = self.viewControllers;
  52. for (CustomNavViewController *navCtrl in controllerArray) {
  53. UIViewController *ctrl = navCtrl.viewControllers[0];
  54. if ([ctrl isKindOfClass:[KSBaseViewController class]]) {
  55. KSBaseViewController *baseCtrl = (KSBaseViewController *)ctrl;
  56. [baseCtrl removeNotifier];
  57. }
  58. }
  59. }
  60. - (CustomNavViewController *)createNavControllerWithClassName:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
  61. KSBaseViewController *controller = [[NSClassFromString(className) alloc] init];
  62. controller.title = title;
  63. controller.tabBarItem.title = title;
  64. controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  65. controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  66. [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x808492),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateNormal];
  67. [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x283240),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateSelected];
  68. CustomNavViewController *navCtrl= [[CustomNavViewController alloc] initWithRootViewController:controller];
  69. return navCtrl;
  70. }
  71. - (void)tabBarSelectedWithIndex:(NSInteger)index {
  72. [self setSelectedIndex:index];
  73. }
  74. - (void)noteNewsWithIndex:(NSInteger)index count:(NSInteger)count { //提醒
  75. UITabBarItem * tabBarItem = self.tabBar.items[index];
  76. if (count == 0) {
  77. [tabBarItem setBadgeValue:nil];// 隐藏角标
  78. }else if (count > 99) {
  79. [tabBarItem setBadgeValue:@"99+"];
  80. } else {
  81. [tabBarItem setBadgeValue:[NSString stringWithFormat:@"%zd",count]];
  82. }
  83. }
  84. - (void)clearNewsWithIndex:(NSInteger)index { //清除
  85. UITabBarItem * tabBarItem = self.tabBar.items[index];
  86. [tabBarItem setBadgeValue:nil];
  87. }
  88. #pragma mark ----- UITabBarControllerDelegate
  89. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
  90. [self setAnaimationWithTabBarController:tabBarController selectViewController:viewController];
  91. }
  92. - (void)setAnaimationWithTabBarController:(UITabBarController *)tabBarController selectViewController:(UIViewController *)viewController {
  93. //1.
  94. NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
  95. __block NSMutableArray <UIView *>*tabBarSwappableImageViews = [NSMutableArray arrayWithCapacity:4];
  96. //2.
  97. for (UIView *tempView in tabBarController.tabBar.subviews) {
  98. if ([tempView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
  99. for (UIImageView *tempImageView in tempView.subviews) {
  100. if ([tempImageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
  101. [tabBarSwappableImageViews addObject:tempImageView];
  102. }
  103. }
  104. }
  105. }
  106. //3.
  107. __block UIView *currentTabBarSwappableImageView = tabBarSwappableImageViews[index];
  108. [AnimationHelper lottieAnimation:currentTabBarSwappableImageView index:index];
  109. }
  110. /*
  111. #pragma mark - Navigation
  112. // In a storyboard-based application, you will often want to do a little preparation before navigation
  113. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  114. // Get the new view controller using [segue destinationViewController].
  115. // Pass the selected object to the new view controller.
  116. }
  117. */
  118. @end