InstitutionTabBarController.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // InstitutionTabBarController.m
  3. // KulexiuForStudent
  4. //
  5. // Created by 王智 on 2023/9/11.
  6. //
  7. #import "InstitutionTabBarController.h"
  8. #import "InstitutionNavController.h"
  9. #import "KSBaseViewController.h"
  10. #import "KSTenantHomeViewController.h"
  11. #import "ChatViewController.h"
  12. #import "InsititutionMineViewController.h"
  13. #import <UIImage+Color.h>
  14. #import "AnimationHelper.h"
  15. #import "MineWorksViewController.h"
  16. #import "CustomNavViewController.h"
  17. @interface InstitutionTabBarController ()<UITabBarControllerDelegate>
  18. @end
  19. @implementation InstitutionTabBarController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view.
  23. self.delegate = self;
  24. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showDraftPage) name:@"DisplayDraft" object:nil];
  25. [self configItems];
  26. }
  27. - (void)showDraftPage {
  28. [self tabBarSelectedWithIndex:2];
  29. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  30. CustomNavViewController *navCtrl = self.selectedViewController;
  31. MineWorksViewController *ctrl = [[MineWorksViewController alloc] init];
  32. [ctrl displayDraft:YES];
  33. [navCtrl pushViewController:ctrl animated:YES];
  34. });
  35. }
  36. - (void)configItems {
  37. // 处理 pad上 tabbar样式
  38. if (@available(iOS 18.0, *)) {
  39. if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad)
  40. {
  41. self.traitOverrides.horizontalSizeClass = UIUserInterfaceSizeClassCompact; //TAB BAR ON BOTTOM
  42. }
  43. }
  44. NSArray *controllerArray = @[@"KSTenantHomeViewController",@"ChatViewController",@"InsititutionMineViewController"];
  45. NSArray *titleArray = @[@"首页",@"通讯",@"我的"];
  46. NSArray *imageArray = @[@"tenant_tab_home_unselected",@"tenant_tab_chat_unselected",@"tetant_tab_mine_unselected"];
  47. NSArray *selectedImgArray = @[@"tenant_tab_home_selected",@"tenant_tab_chat_selected",@"tetant_tab_mine_selected"];
  48. NSMutableArray *ctrlArray = [NSMutableArray array];
  49. for (NSInteger i = 0; i < controllerArray.count; i++) {
  50. CustomNavViewController *navCtrl = [self createNavControllerWithClassName:controllerArray[i] title:titleArray[i] imageName:imageArray[i] selectedImageName:selectedImgArray[i]];
  51. [ctrlArray addObject:navCtrl];
  52. }
  53. self.viewControllers = [NSArray arrayWithArray:ctrlArray];
  54. if (@available(iOS 13.0, *)) {
  55. [[UITabBar appearance] setUnselectedItemTintColor:HexRGB(0x999999)];
  56. [[UITabBar appearance] setTintColor:INS_THEMECOLOR];
  57. } else {
  58. [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0x999999),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateNormal];
  59. [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:INS_THEMECOLOR,NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateSelected];
  60. }
  61. if (@available(iOS 15.0, *)) {
  62. // 适配Xcode 13 iOS 15tabbar透明的问题
  63. UITabBarAppearance * appearance = [[UITabBarAppearance alloc] init];
  64. [appearance configureWithOpaqueBackground];
  65. appearance.backgroundColor = UIColor.whiteColor;
  66. appearance.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
  67. [UITabBar appearance].scrollEdgeAppearance = appearance;
  68. [UITabBar appearance].standardAppearance = appearance;
  69. }
  70. [UITabBar appearance].backgroundColor = UIColor.whiteColor;
  71. self.tabBar.translucent = NO;
  72. [self changeTabBarColor:HexRGB(0x121B2E)];
  73. }
  74. - (CustomNavViewController *)createNavControllerWithClassName:(NSString *)className title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
  75. KSBaseViewController *controller = [[NSClassFromString(className) alloc] init];
  76. controller.title = title;
  77. controller.tabBarItem.title = title;
  78. controller.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  79. controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  80. [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0xAAAAAA),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateNormal];
  81. [controller.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:HexRGB(0xFF497B),NSFontAttributeName:[UIFont systemFontOfSize:10 weight:UIFontWeightMedium]} forState:UIControlStateSelected];
  82. CustomNavViewController *navCtrl= [[CustomNavViewController alloc] initWithRootViewController:controller];
  83. return navCtrl;
  84. }
  85. - (void)changeBgColorWithTabBarController:(UITabBarController *)tabBarController selectViewController:(UIViewController *)viewController {
  86. if ([viewController isKindOfClass:[CustomNavViewController class]]) {
  87. CustomNavViewController *navCtrl = (CustomNavViewController *)viewController;
  88. UIViewController *childrenCtrl = [navCtrl.visibleViewController.childViewControllers lastObject];
  89. if ([childrenCtrl isMemberOfClass:NSClassFromString(@"TenantDarkViewController")]) {
  90. [self changeTabBarColor:HexRGB(0x121B2E)];
  91. }
  92. else {
  93. [self resetTabBarColor];
  94. }
  95. }
  96. }
  97. - (void)tabBarSelectedWithIndex:(NSInteger)index {
  98. if (index >= 2) { // 跳转到聊天
  99. index -= 1;
  100. }
  101. [self setSelectedIndex:index];
  102. UIViewController *controller = [self.viewControllers objectAtIndex:index];
  103. [self changeBgColorWithTabBarController:self selectViewController:controller];
  104. }
  105. #pragma mark ----- UITabBarControllerDelegate
  106. - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
  107. BOOL isDoubleClick = [self checkIsDoubleClick:viewController];
  108. if ([viewController isEqual:[tabBarController.viewControllers objectAtIndex:2]] && isDoubleClick) {
  109. [[NSNotificationCenter defaultCenter] postNotificationName:CHATVIEW_REFRESH object:nil];
  110. }
  111. [self changeBgColorWithTabBarController:tabBarController selectViewController:viewController];
  112. // [self setAnaimationWithTabBarController:tabBarController selectViewController:viewController];
  113. }
  114. - (void)setAnaimationWithTabBarController:(UITabBarController *)tabBarController selectViewController:(UIViewController *)viewController {
  115. //1.
  116. NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];
  117. __block NSMutableArray <UIView *>*tabBarSwappableImageViews = [NSMutableArray arrayWithCapacity:4];
  118. //2.
  119. for (UIView *tempView in tabBarController.tabBar.subviews) {
  120. if ([tempView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
  121. for (UIImageView *tempImageView in tempView.subviews) {
  122. if ([tempImageView isKindOfClass:NSClassFromString(@"UITabBarSwappableImageView")]) {
  123. [tabBarSwappableImageViews addObject:tempImageView];
  124. }
  125. }
  126. }
  127. }
  128. //3.
  129. __block UIView *currentTabBarSwappableImageView = tabBarSwappableImageViews[index];
  130. [AnimationHelper lottieAnimation:currentTabBarSwappableImageView index:index];
  131. }
  132. - (BOOL)checkIsDoubleClick:(UIViewController *)viewController {
  133. static UIViewController *lastViewController = nil;
  134. static NSTimeInterval lastClickTime = 0;
  135. if (lastViewController != viewController) {
  136. lastViewController = viewController;
  137. lastClickTime = [NSDate timeIntervalSinceReferenceDate];
  138. return NO;
  139. }
  140. NSTimeInterval clickTime = [NSDate timeIntervalSinceReferenceDate];
  141. if (clickTime - lastClickTime > 0.5) {
  142. lastClickTime = clickTime;
  143. return NO;
  144. }
  145. lastViewController = nil;
  146. lastClickTime = 0;
  147. return YES;
  148. }
  149. - (void)dealloc {
  150. [[NSNotificationCenter defaultCenter] removeObserver:self];
  151. }
  152. /*
  153. #pragma mark - Navigation
  154. // In a storyboard-based application, you will often want to do a little preparation before navigation
  155. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  156. // Get the new view controller using [segue destinationViewController].
  157. // Pass the selected object to the new view controller.
  158. }
  159. */
  160. @end