UITabBarController自定义一
- 首先在Appdelegate.m文件中将UITabBarController的子类设置为rootViewController,并设置其viewControllers
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
JKBaseTabBarController *baseTabBarController = [self createRootController];
self.window.rootViewController = baseTabBarController;
return YES;
}
#pragma mark - 创建跟视图
-(JKBaseTabBarController *)createRootController{
JKBaseTabBarController *baseTabBarController = [[JKBaseTabBarController alloc] init];
JKGeneralViewController *general = [[JKGeneralViewController alloc] init];
general.tabBarItem.title = @"总览";
general.tabBarItem.image = [[UIImage imageNamed:@"overview_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
general.tabBarItem.selectedImage = [[UIImage imageNamed:@"overview_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
JKAccountViewController *account = [[JKAccountViewController alloc] init];
account.tabBarItem.title = @"账户";
account.tabBarItem.image = [[UIImage imageNamed:@"account_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
account.tabBarItem.selectedImage = [[UIImage imageNamed:@"account_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
JKGraphViewController *graph = [[JKGraphViewController alloc] init];
graph.tabBarItem.title = @"图表";
graph.tabBarItem.image = [[UIImage imageNamed:@"graph_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
graph.tabBarItem.selectedImage = [[UIImage imageNamed:@"graph_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
baseTabBarController.viewControllers = @[general,account,graph];
return baseTabBarController;
}
- 然后在UITabBarController子类的viewWillAppear方法中将tabBar的子视图隐藏掉,换上自定义的视图
#pragma mark - 自定义
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.tabBar.barTintColor = [UIColor blackColor];
for (UIView *view in self.tabBar.subviews) {
view.hidden = YES;
}
NSArray *controllers = self.viewControllers;
CGFloat h = self.tabBar.bounds.size.height;
CGFloat w = h;
CGFloat interval = (s_w - controllers.count * h) / (controllers.count + 1);
for (int i = 0; i < controllers.count; i++) {
UITabBarItem *item = ((UIViewController *)controllers[i]).tabBarItem;
UIButton *btn = [[UIButton alloc] initWithFrame:(CGRect){interval + i * (w + interval),0,w,h}];
btn.imageEdgeInsets = UIEdgeInsetsMake(-15, 0, 0, 0);
[btn setImage:item.image forState:UIControlStateNormal];
[btn setImage:item.selectedImage forState:UIControlStateSelected];
btn.titleEdgeInsets = UIEdgeInsetsMake(30, -35, 0, 0);
btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
[btn setTitle:item.title forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn setTitleColor:lightBlueColor forState:UIControlStateSelected];
btn.tag = i + 200;
[btn addTarget:self action:@selector(changeViewController:) forControlEvents:UIControlEventTouchUpInside];
if (i == 0) {
btn.selected = YES;
}
[self.tabBar addSubview:btn];
}
}
-(void)changeViewController:(UIButton *)sender{
if (!sender.selected) {
sender.selected = !sender.selected;
self.selectedIndex = sender.tag - 200;
for (UIView *view in self.tabBar.subviews) {
if ([view isKindOfClass:[UIButton class]] && view.tag != sender.tag) {
UIButton *btn = (UIButton *)view;
btn.selected = NO;
}
}
}
}
- 效果如图
*没有上诉自定义的话也可以通过
[general.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} forState:UIControlStateNormal];
方法改变title的颜色