TabBarController.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // TabBarController.m
  3. // TCXF
  4. //
  5. // Created by 张毅成 on 2017/6/22.
  6. // Copyright © 2017年 张毅成. All rights reserved.
  7. //
  8. #import "TabBarController.h"
  9. #import "NavigationController.h"
  10. #import "WorkFlowController.h"
  11. #import "MyViewController.h"
  12. #import "AppDelegate.h"
  13. #import "My_CenterVC.h"
  14. #import "ChatMsgListVC.h"
  15. #import "CommonHomeVC.h"
  16. #import "IndexViewController.h"
  17. @interface TabBarController ()<UITabBarControllerDelegate>
  18. @property (nonatomic, strong) UIView * dotImage;
  19. @end
  20. @implementation TabBarController
  21. TDShareInstance_implementation(TabBarController)
  22. - (instancetype)init {
  23. if (self = [super init]) {
  24. [self addchlidVcs];
  25. }
  26. return self;
  27. }
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. self.delegate = self;
  31. [self setTabBarItemStyle];
  32. //池塘红点
  33. }
  34. /// 选择的当前控制器是否可以旋转
  35. -(BOOL)shouldAutorotate{
  36. return [self.selectedViewController shouldAutorotate];
  37. }
  38. /// 选择的当前控制器是支持的旋转的方向
  39. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
  40. return [self.selectedViewController supportedInterfaceOrientations];
  41. }
  42. ///
  43. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
  44. return [self.selectedViewController preferredInterfaceOrientationForPresentation];
  45. }
  46. /**
  47. * 根据参数, 创建并添加对应的子控制器
  48. *
  49. * @param vc 需要添加的控制器(会自动包装导航控制器)
  50. * @param isRequired 标题
  51. * @param normalImageName 一般图片名称
  52. * @param selectedImageName 选中图片名称
  53. */
  54. - (void)addChildVC: (UIViewController *)vc title:(NSString *)title normalImageName: (NSString *)normalImageName selectedImageName:(NSString *)selectedImageName isRequiredNavController: (BOOL)isRequired {
  55. // vc.view.backgroundColor = [UIColor whiteColor];
  56. if (isRequired) {
  57. NavigationController *nav = [[NavigationController alloc] initWithRootViewController:vc];
  58. UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[self originImageWithName:normalImageName] selectedImage:[self originImageWithName:selectedImageName]];
  59. nav.tabBarItem = tabBarItem;
  60. vc.title = title;
  61. [self addChildViewController:nav];
  62. }else {
  63. if([vc isKindOfClass:UINavigationController.class]){
  64. UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[self originImageWithName:normalImageName] selectedImage:[self originImageWithName:selectedImageName]];
  65. vc.tabBarItem = tabBarItem;
  66. vc.title = title;
  67. }
  68. [self addChildViewController:vc];
  69. }
  70. }
  71. - (void)addchlidVcs {
  72. // [self addChildVC:[self className:@"HomeTestViewController"] title:@"首页" normalImageName:@"bar_home_off" selectedImageName:@"bar_home_on" isRequiredNavController:true];
  73. // [self addChildVC:[self className:@"HomeViewController"] title:@"首页" normalImageName:@"bar_home_off" selectedImageName:@"bar_home_on" isRequiredNavController:true];
  74. [self addChildVC:[self className:@"IndexViewController"] title:@"首页" normalImageName:@"bar_home_off" selectedImageName:@"bar_home_on" isRequiredNavController:true];
  75. // [self addChildVC:[self className:@"WorkFlowController"] title:@"工作" normalImageName:@"bar_work_off" selectedImageName:@"bar_work_on" isRequiredNavController:true];
  76. // [self addChildVC:[self className:@"ChatMessageController"] title:@"信源" normalImageName:@"bar_chat_off" selectedImageName:@"bar_chat_on" isRequiredNavController:true];
  77. [self addChildVC:[StoryboardManager shared].Common.instantiateInitialViewController title:@"常用" normalImageName:@"bar_work_off" selectedImageName:@"bar_work_on" isRequiredNavController:false];
  78. [self addChildVC:[StoryboardManager shared].Source.instantiateInitialViewController title:@"信源" normalImageName:@"bar_chat_off" selectedImageName:@"bar_chat_on" isRequiredNavController:false];
  79. // [self addChildVC:[StoryboardManager shared].chatMsg.instantiateInitialViewController title:@"信源" normalImageName:@"bar_chat_off" selectedImageName:@"bar_chat_on" isRequiredNavController:false];
  80. [self addChildVC:[StoryboardManager shared].myCenter.instantiateInitialViewController title:@"我的" normalImageName:@"bar_my_off" selectedImageName:@"bar_my_on" isRequiredNavController:false];
  81. }
  82. - (void)setTabBarItemStyle{
  83. UITabBarItem *tabBarItem = [UITabBarItem appearance];
  84. [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:k6} forState:UIControlStateNormal];
  85. [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:kMainColor} forState:UIControlStateSelected];
  86. }
  87. - (instancetype)className:(NSString *)className {
  88. id controller = [NSClassFromString(className) new];
  89. return controller;
  90. }
  91. - (void)setSelectedIndex:(NSUInteger)selectedIndex {
  92. [super setSelectedIndex:selectedIndex];
  93. }
  94. - (UIImage *)originImageWithName:(NSString *)imageName{
  95. return [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  96. }
  97. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  98. // 确保当前显示的是首页界面,本例中首页是第一个 tabBar
  99. if ([tabBarController.selectedViewController isEqual:[tabBarController.viewControllers firstObject]]) {
  100. // 再次选中的 tab 页面是之前上一次选中的控制器页面
  101. if ([viewController isEqual:tabBarController.selectedViewController]) {
  102. // 发送通知,让首页刷新数据
  103. [[NSNotificationCenter defaultCenter] postNotificationName:HOMESCROLLVIEWTOTOP object:nil];
  104. return NO;
  105. }
  106. }
  107. return YES;
  108. }
  109. - (void)didReceiveMemoryWarning {
  110. [super didReceiveMemoryWarning];
  111. // Dispose of any resources that can be recreated.
  112. }
  113. #pragma mark 池塘红点
  114. - (void)chatMsgRedPoint:(NSInteger)number {
  115. [self.dotImage removeFromSuperview];
  116. if(number == 0) {
  117. return;
  118. }else{
  119. CGFloat redPointX = (SCREEN_WIDTH / 4) * 3 - (SCREEN_WIDTH / 4)/2;
  120. CGFloat x = ceilf(redPointX) + 5;
  121. CGFloat y = 5;
  122. self.dotImage = [[UIView alloc] initWithFrame:CGRM(x, y, 10, 10)];
  123. UILabel *numLabel = [[UILabel alloc] init];
  124. self.dotImage.width = 16;
  125. self.dotImage.height = 16;
  126. self.dotImage.layer.cornerRadius = 8;
  127. [self.dotImage addSubview:numLabel];
  128. numLabel.frame = CGRectMake(0, 0, self.dotImage.width, self.dotImage.height);
  129. numLabel.font = [UIFont systemFontOfSize:11.0];
  130. numLabel.textAlignment = NSTextAlignmentCenter;
  131. numLabel.textColor = RGB(255, 255, 255);
  132. numLabel.text = [NSString stringWithFormat:@"%ld",(long)number];
  133. self.dotImage.backgroundColor = RGB(255, 82, 82);
  134. [self.tabBar addSubview:self.dotImage];
  135. }
  136. }
  137. @end