// // TabBarController.m // TCXF // // Created by 张毅成 on 2017/6/22. // Copyright © 2017年 张毅成. All rights reserved. // #import "TabBarController.h" #import "NavigationController.h" #import "HomeViewController.h" #import "WorkFlowController.h" #import "MyViewController.h" #import "AppDelegate.h" #import "My_CenterVC.h" #import "ChatMsgListVC.h" #import "HomeTestViewController.h" @interface TabBarController () @property (nonatomic, strong) UIView * dotImage; @end @implementation TabBarController TDShareInstance_implementation(TabBarController) - (instancetype)init { if (self = [super init]) { [self addchlidVcs]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.delegate = self; [self setTabBarItemStyle]; //池塘红点 } /// 选择的当前控制器是否可以旋转 -(BOOL)shouldAutorotate{ return [self.selectedViewController shouldAutorotate]; } /// 选择的当前控制器是支持的旋转的方向 - (UIInterfaceOrientationMask)supportedInterfaceOrientations{ return [self.selectedViewController supportedInterfaceOrientations]; } /// - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [self.selectedViewController preferredInterfaceOrientationForPresentation]; } /** * 根据参数, 创建并添加对应的子控制器 * * @param vc 需要添加的控制器(会自动包装导航控制器) * @param isRequired 标题 * @param normalImageName 一般图片名称 * @param selectedImageName 选中图片名称 */ - (void)addChildVC: (UIViewController *)vc title:(NSString *)title normalImageName: (NSString *)normalImageName selectedImageName:(NSString *)selectedImageName isRequiredNavController: (BOOL)isRequired { // vc.view.backgroundColor = [UIColor whiteColor]; if (isRequired) { NavigationController *nav = [[NavigationController alloc] initWithRootViewController:vc]; UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[self originImageWithName:normalImageName] selectedImage:[self originImageWithName:selectedImageName]]; nav.tabBarItem = tabBarItem; vc.title = title; [self addChildViewController:nav]; }else { if([vc isKindOfClass:UINavigationController.class]){ UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:[self originImageWithName:normalImageName] selectedImage:[self originImageWithName:selectedImageName]]; vc.tabBarItem = tabBarItem; vc.title = title; } [self addChildViewController:vc]; } } - (void)addchlidVcs { // [self addChildVC:[self className:@"HomeTestViewController"] title:@"首页" normalImageName:@"bar_home_off" selectedImageName:@"bar_home_on" isRequiredNavController:true]; [self addChildVC:[self className:@"HomeViewController"] title:@"首页" normalImageName:@"bar_home_off" selectedImageName:@"bar_home_on" isRequiredNavController:true]; [self addChildVC:[self className:@"WorkFlowController"] title:@"工作" normalImageName:@"bar_work_off" selectedImageName:@"bar_work_on" isRequiredNavController:true]; // [self addChildVC:[self className:@"ChatMessageController"] title:@"信源" normalImageName:@"bar_chat_off" selectedImageName:@"bar_chat_on" isRequiredNavController:true]; [self addChildVC:[StoryboardManager shared].chatMsg.instantiateInitialViewController title:@"信源" normalImageName:@"bar_chat_off" selectedImageName:@"bar_chat_on" isRequiredNavController:false]; [self addChildVC:[StoryboardManager shared].myCenter.instantiateInitialViewController title:@"我的" normalImageName:@"bar_my_off" selectedImageName:@"bar_my_on" isRequiredNavController:false]; } - (void)setTabBarItemStyle{ UITabBarItem *tabBarItem = [UITabBarItem appearance]; [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:k6} forState:UIControlStateNormal]; [tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:kMainColor} forState:UIControlStateSelected]; } - (instancetype)className:(NSString *)className { id controller = [NSClassFromString(className) new]; return controller; } - (void)setSelectedIndex:(NSUInteger)selectedIndex { [super setSelectedIndex:selectedIndex]; } - (UIImage *)originImageWithName:(NSString *)imageName{ return [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; } - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { // 确保当前显示的是首页界面,本例中首页是第一个 tabBar if ([tabBarController.selectedViewController isEqual:[tabBarController.viewControllers firstObject]]) { // 再次选中的 tab 页面是之前上一次选中的控制器页面 if ([viewController isEqual:tabBarController.selectedViewController]) { // 发送通知,让首页刷新数据 [[NSNotificationCenter defaultCenter] postNotificationName:HOMESCROLLVIEWTOTOP object:nil]; return NO; } } return YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark 池塘红点 - (void)chatMsgRedPoint:(NSInteger)number { [self.dotImage removeFromSuperview]; if(number == 0) { return; }else{ CGFloat redPointX = (SCREEN_WIDTH / 4) * 3 - (SCREEN_WIDTH / 4)/2; CGFloat x = ceilf(redPointX) + 5; CGFloat y = 5; self.dotImage = [[UIView alloc] initWithFrame:CGRM(x, y, 10, 10)]; UILabel *numLabel = [[UILabel alloc] init]; self.dotImage.width = 16; self.dotImage.height = 16; self.dotImage.layer.cornerRadius = 8; [self.dotImage addSubview:numLabel]; numLabel.frame = CGRectMake(0, 0, self.dotImage.width, self.dotImage.height); numLabel.font = [UIFont systemFontOfSize:11.0]; numLabel.textAlignment = NSTextAlignmentCenter; numLabel.textColor = RGB(255, 255, 255); numLabel.text = [NSString stringWithFormat:@"%ld",(long)number]; self.dotImage.backgroundColor = RGB(255, 82, 82); [self.tabBar addSubview:self.dotImage]; } } @end