TabBarController.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "HomeViewController.h"
  11. #import "WorkFlowController.h"
  12. #import "MyViewController.h"
  13. #import "AppDelegate.h"
  14. #import "My_CenterVC.h"
  15. #import "ChatMsgListVC.h"
  16. #import "HomeTestViewController.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:@"WorkFlowController"] title:@"工作" normalImageName:@"bar_work_off" selectedImageName:@"bar_work_on" isRequiredNavController:true];
  75. // [self addChildVC:[self className:@"ChatMessageController"] title:@"信源" normalImageName:@"bar_chat_off" selectedImageName:@"bar_chat_on" isRequiredNavController:true];
  76. [self addChildVC:[StoryboardManager shared].chatMsg.instantiateInitialViewController title:@"信源" normalImageName:@"bar_chat_off" selectedImageName:@"bar_chat_on" isRequiredNavController:false];
  77. [self addChildVC:[StoryboardManager shared].myCenter.instantiateInitialViewController title:@"我的" normalImageName:@"bar_my_off" selectedImageName:@"bar_my_on" isRequiredNavController:false];
  78. }
  79. - (void)setTabBarItemStyle{
  80. UITabBarItem *tabBarItem = [UITabBarItem appearance];
  81. [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:k6} forState:UIControlStateNormal];
  82. [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:kMainColor} forState:UIControlStateSelected];
  83. }
  84. - (instancetype)className:(NSString *)className {
  85. id controller = [NSClassFromString(className) new];
  86. return controller;
  87. }
  88. - (void)setSelectedIndex:(NSUInteger)selectedIndex {
  89. [super setSelectedIndex:selectedIndex];
  90. }
  91. - (UIImage *)originImageWithName:(NSString *)imageName{
  92. return [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  93. }
  94. - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
  95. // 确保当前显示的是首页界面,本例中首页是第一个 tabBar
  96. if ([tabBarController.selectedViewController isEqual:[tabBarController.viewControllers firstObject]]) {
  97. // 再次选中的 tab 页面是之前上一次选中的控制器页面
  98. if ([viewController isEqual:tabBarController.selectedViewController]) {
  99. // 发送通知,让首页刷新数据
  100. [[NSNotificationCenter defaultCenter] postNotificationName:HOMESCROLLVIEWTOTOP object:nil];
  101. return NO;
  102. }
  103. }
  104. return YES;
  105. }
  106. - (void)didReceiveMemoryWarning {
  107. [super didReceiveMemoryWarning];
  108. // Dispose of any resources that can be recreated.
  109. }
  110. #pragma mark 池塘红点
  111. - (void)chatMsgRedPoint:(NSInteger)number {
  112. [self.dotImage removeFromSuperview];
  113. if(number == 0) {
  114. return;
  115. }else{
  116. CGFloat redPointX = (SCREEN_WIDTH / 4) * 3 - (SCREEN_WIDTH / 4)/2;
  117. CGFloat x = ceilf(redPointX) + 5;
  118. CGFloat y = 5;
  119. self.dotImage = [[UIView alloc] initWithFrame:CGRM(x, y, 10, 10)];
  120. UILabel *numLabel = [[UILabel alloc] init];
  121. self.dotImage.width = 16;
  122. self.dotImage.height = 16;
  123. self.dotImage.layer.cornerRadius = 8;
  124. [self.dotImage addSubview:numLabel];
  125. numLabel.frame = CGRectMake(0, 0, self.dotImage.width, self.dotImage.height);
  126. numLabel.font = [UIFont systemFontOfSize:11.0];
  127. numLabel.textAlignment = NSTextAlignmentCenter;
  128. numLabel.textColor = RGB(255, 255, 255);
  129. numLabel.text = [NSString stringWithFormat:@"%ld",(long)number];
  130. self.dotImage.backgroundColor = RGB(255, 82, 82);
  131. [self.tabBar addSubview:self.dotImage];
  132. }
  133. }
  134. @end