TabBarController.m 5.8 KB

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