JYTabBarController.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // JYTabBarController.m
  3. // JianYuIOS
  4. //
  5. // Created by apple on 2018/11/8.
  6. // Copyright © 2018年 lixianglan. All rights reserved.
  7. //
  8. #import "JYTabBarController.h"
  9. #import "JYNavigationController.h"
  10. #import "JYSearchViewController.h"
  11. #import "JYSubViewController.h"
  12. #import "JYExpViewController.h"
  13. #import "JYMineViewController.h"
  14. #import "ViewController1.h"
  15. #import "ViewController2.h"
  16. #import "ViewController3.h"
  17. #import "ViewController4.h"
  18. @interface JYTabBarController ()<UITabBarControllerDelegate>
  19. @property (nonatomic,strong)JYSearchViewController *V1;
  20. @property (nonatomic,strong)JYSubViewController *V2;
  21. @property (nonatomic,strong)JYExpViewController *V3;
  22. @property (nonatomic,strong)JYMineViewController *V4;
  23. @property (nonatomic,strong)ViewController1 *VV1;
  24. @property (nonatomic,strong)ViewController2 *VV2;
  25. @property (nonatomic,strong)ViewController3 *VV3;
  26. @property (nonatomic,strong)ViewController4 *VV4;
  27. @end
  28. @implementation JYTabBarController
  29. +(void)initialize
  30. {
  31. if (@available(iOS 9.0, *)) {
  32. UITabBarItem *tabBarItem = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
  33. NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
  34. dictNormal[NSForegroundColorAttributeName] = UIColorFromRGB(0x888888);
  35. dictNormal[NSFontAttributeName] = [UIFont systemFontOfSize:10];
  36. NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
  37. dictSelected[NSForegroundColorAttributeName] = UIColorFromRGB(0x2cb7ca);
  38. dictSelected[NSFontAttributeName] = [UIFont systemFontOfSize:10];
  39. [tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
  40. [tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
  41. } else {
  42. // Fallback on earlier versions
  43. }
  44. }
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. // Do any additional setup after loading the view.
  48. self.delegate = self;
  49. self.tabBarController.tabBar.delegate = self;
  50. [self setUpAllChildVc];
  51. [self setSelectedIndex:0];
  52. [self switchRootViewController];
  53. }
  54. //判断是否跳转
  55. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
  56. NSLog(@"点击了第几个item:%ld",tabBarController.tabBar.selectedItem.tag);
  57. // if (![LXUserDefaults token]) {
  58. // NSString *index = [NSString stringWithFormat:@"%ld",tabBarController.tabBar.selectedItem.tag];
  59. // [[NSNotificationCenter defaultCenter] postNotificationName:@"isLoginTabBar" object:nil userInfo:@{@"item_index":index}];
  60. // return NO;
  61. // }
  62. return YES;
  63. }
  64. - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
  65. // NSLog(@"点击了第几个item:%ld",item.tag);
  66. }
  67. #pragma mark - -----------------------------------------
  68. - (void)setUpAllChildVc
  69. {
  70. self.V1 = [[JYSearchViewController alloc] init];
  71. [self setUpOneChildVcWithVc:self.V1 Image:@"search" selectedImage:@"search1" title:@"搜索" tag:0];
  72. self.V2 = [[JYSubViewController alloc] init];
  73. [self setUpOneChildVcWithVc:self.V2 Image:@"subscribe" selectedImage:@"subscribe1" title:@"订阅" tag:1];
  74. self.V3 = [[JYExpViewController alloc] init];
  75. [self setUpOneChildVcWithVc:self.V3 Image:@"laboratory" selectedImage:@"laboratory1" title:@"实验室" tag:2];
  76. self.V4 = [[JYMineViewController alloc] init];
  77. [self setUpOneChildVcWithVc:self.V4 Image:@"mine" selectedImage:@"mine1" title:@"我的" tag:3];
  78. // self.VV1 = [[ViewController1 alloc] init];
  79. // [self setUpOneChildVcWithVc:self.VV1 Image:@"icon-home" selectedImage:@"icon-home2" title:@"搜索" tag:0];
  80. //
  81. // self.VV2 = [[ViewController2 alloc] init];
  82. // [self setUpOneChildVcWithVc:self.VV2 Image:@"icon-home" selectedImage:@"icon-home2" title:@"订阅" tag:1];
  83. //
  84. // self.VV3 = [[ViewController3 alloc] init];
  85. // [self setUpOneChildVcWithVc:self.VV3 Image:@"icon-home" selectedImage:@"icon-home2" title:@"实验室" tag:2];
  86. // self.VV4 = [[ViewController4 alloc] init];
  87. // [self setUpOneChildVcWithVc:self.VV4 Image:@"icon-home" selectedImage:@"icon-home2" title:@"我的" tag:3];
  88. }
  89. #pragma mark - 初始化设置tabBar上面单个按钮的方法
  90. /**
  91. *
  92. * 设置单个tabBarButton
  93. *
  94. * @param VC 每一个按钮对应的控制器
  95. * @param image 每一个按钮对应的普通状态下图片
  96. * @param selectedImage 每一个按钮对应的选中状态下的图片
  97. * @param title 每一个按钮对应的标题
  98. */
  99. - (void)setUpOneChildVcWithVc:(UIViewController *)VC Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title tag:(NSInteger)current_tag
  100. {
  101. JYNavigationController *nav = [[JYNavigationController alloc] initWithRootViewController:VC];
  102. UIImage *myImage = [UIImage imageNamed:image];
  103. myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  104. //tabBarItem,是系统提供模型,专门负责tabbar上按钮的文字以及图片展示
  105. VC.tabBarItem.image = myImage;
  106. UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
  107. mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  108. VC.tabBarItem.selectedImage = mySelectedImage;
  109. VC.tabBarItem.title = title;
  110. // VC.navigationItem.title = title;
  111. VC.tabBarItem.tag = current_tag;
  112. [self addChildViewController:nav];
  113. }
  114. -(UIColor *)randomColor
  115. {
  116. return [UIColor whiteColor];
  117. }
  118. #pragma mark - 根控制器的切换
  119. -(void)switchRootViewController
  120. {
  121. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeItems:) name:@"changeItems" object:nil];
  122. }
  123. -(void)changeItems:(NSNotification *)notification
  124. {
  125. NSLog(@"收到改变tabbar的通知");
  126. NSDictionary *dict = notification.userInfo;
  127. NSInteger index = [dict[@"item_index"] integerValue];
  128. [self setSelectedIndex:index];
  129. // [self.V1 dealTabBar];
  130. // [self.V1 dealTabBar];
  131. // [self.V1 dealTabBar];
  132. // [self.V1 dealTabBar];
  133. if(index==1) {
  134. [self.V2 dealTabBar];
  135. }else if (index==2) {
  136. [self.V3 dealTabBar];
  137. }else if (index==3) {
  138. [self.V4 dealTabBar];
  139. }else {
  140. }
  141. }
  142. - (void)didReceiveMemoryWarning {
  143. [super didReceiveMemoryWarning];
  144. // Dispose of any resources that can be recreated.
  145. }
  146. /*
  147. #pragma mark - Navigation
  148. // In a storyboard-based application, you will often want to do a little preparation before navigation
  149. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  150. // Get the new view controller using [segue destinationViewController].
  151. // Pass the selected object to the new view controller.
  152. }
  153. */
  154. @end