//.h文件
#import <UIKit/UIKit.h>
@interface MyTabBarController : UITabBarController
@end
// .m文件
#import "MyTabBarController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourViewController.h"
// 自定义标签栏控制器
@interface MyTabBarController ()
@property(nonatomic,strong)FirstViewController * first;
@property(nonatomic,strong)SecondViewController * second;
@property(nonatomic,strong)ThirdViewController * third;
@property(nonatomic,strong) FourViewController * four;
@end
@implementation MyTabBarController
- (void) initView
{
// 标题
NSArray * arr_title = [NSArray arrayWithObjects:@"时尚圈",@"账单",@"我的",@"其他", nil];
// 未选中状态的image
NSArray * arr_unselected = [NSArray arrayWithObjects:[UIImage imageNamed:@"item_xuexinbao"],[UIImage imageNamed:@"item_zhangdan"],[UIImage imageNamed:@"item_xinyong"],[UIImage imageNamed:@"item_account"], nil];
// 选中状态的image
// UIImage * image = [[UIImage imageNamed:@"item_xuexinbao_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
// ios7新特性 image的渲染效果(三种:1.根据上下文环境决定image的颜色 2.保持image原有的颜色 3.根据tintColor决定image的颜色)
// 使用这个新特性 让每一个item的选中状态的图片保留原有的颜色
NSArray * arr_selected = [NSArray arrayWithObjects:[[UIImage imageNamed:@"item_xuexinbao_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ],[[UIImage imageNamed:@"item_zhangdan_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal],[[UIImage imageNamed:@"item_xinyong_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal],[[UIImage imageNamed:@"item_account_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal], nil];
_first = [[FirstViewController alloc] init];
_first.title = [arr_title objectAtIndex:0];
UINavigationController * firstNC = [[UINavigationController alloc] initWithRootViewController:_first];
_second = [[SecondViewController alloc] init];
_second.title = [arr_title objectAtIndex:1];
UINavigationController * secondNC = [[UINavigationController alloc] initWithRootViewController:_second];
_third = [[ThirdViewController alloc] init];
_third.title = [arr_title objectAtIndex:2];
UINavigationController * thirdNC = [[UINavigationController alloc] initWithRootViewController:_third];
_four = [[FourViewController alloc] init];
_four.title = [arr_title objectAtIndex:3];
UINavigationController * fourNC = [[UINavigationController alloc] initWithRootViewController:_four];
if (!ios7) {
// 如果是ios7.0 以下的版本 设置标题和图片
_first.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:0] image:nil tag:0];
[_first.tabBarItem setFinishedSelectedImage:[arr_selected objectAtIndex:0] withFinishedUnselectedImage:[arr_unselected objectAtIndex:0]];
_second.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:1] image:nil tag:1];
[_second.tabBarItem setFinishedSelectedImage:[arr_selected objectAtIndex:1] withFinishedUnselectedImage:[arr_unselected objectAtIndex:1]];
_third.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:2] image:nil tag:2];
[_third.tabBarItem setFinishedSelectedImage:[arr_selected objectAtIndex:2] withFinishedUnselectedImage:[arr_unselected objectAtIndex:2]];
_four.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:3] image:nil tag:3];
[_four.tabBarItem setFinishedSelectedImage:[arr_selected objectAtIndex:3] withFinishedUnselectedImage:[arr_unselected objectAtIndex:3]];
}else{
// 如果是ios7.0 以上的版本 设置标题和图片
_first.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:0] image:[arr_unselected objectAtIndex:0] selectedImage:[arr_selected objectAtIndex:0]];
_second.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:1] image:[arr_unselected objectAtIndex:1] selectedImage:[arr_selected objectAtIndex:1]];
_third.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:2] image:[arr_unselected objectAtIndex:2] selectedImage:[arr_selected objectAtIndex:2]];
_four.tabBarItem = [[UITabBarItem alloc] initWithTitle:[arr_title objectAtIndex:3] image:[arr_unselected objectAtIndex:3] selectedImage:[arr_selected objectAtIndex:3]];
}
// 设置每一个item 选中 和 非选中 字体颜色和字体大小
[self unselectedTapTabBarItem:_first.tabBarItem];
[self selectedTapTabBarItem:_first.tabBarItem];
[self unselectedTapTabBarItem:_second.tabBarItem];
[self selectedTapTabBarItem:_second.tabBarItem];
[self unselectedTapTabBarItem:_third.tabBarItem];
[self selectedTapTabBarItem:_third.tabBarItem];
[self unselectedTapTabBarItem:_four.tabBarItem];
[self selectedTapTabBarItem:_four.tabBarItem];
self.viewControllers = @[firstNC,secondNC,thirdNC,fourNC];
// 默认颜色 是蓝色 要更改颜色
}
// 未选中 点击
- (void)unselectedTapTabBarItem:(UITabBarItem*)item
{
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:16]} forState:UIControlStateNormal];
}
// 选中 点击
- (void)selectedTapTabBarItem:(UITabBarItem*)item
{
[item setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor orangeColor],NSFontAttributeName:[UIFont systemFontOfSize:16]} forState:UIControlStateSelected];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tabBar.barTintColor = KTabBarItemTintColor;// ios7 新特性 原:tintColor
[self initView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}