- tabBar的定义
//创建第一个导航视图控制器管理的根视图控制器
FirstViewController *_firstVC = [[FirstViewController alloc]init];
//创建第一个导航视图控制器对象,用来管理——firstvc
UINavigationController *_firstNC = [[UINavigationController alloc]initWithRootViewController:_firstVC];
//创建第二个导航视图控制器管理的根视图控制器
ScondViewController *_secondVC = [[ScondViewController alloc]init];
//创建第2个导航视图控制器对象,用来管理_secondVC
UINavigationController *_secondNC = [[UINavigationController alloc]initWithRootViewController:_secondVC];
//这里只上传了两个,其他的可以类推,本demo中共创建了6个
//创建标签视图控制器对象
UITabBarController *_tabBarController = [[UITabBarController alloc]init];
//给标签视图控制器添加所管理的视图控制器 (导航控制器)
_tabBarController.viewControllers = @[_firstNC,_secondNC,_thirdNC,_forthNC,_fifthNC,_sixthNC];
//2.改变tabBar自身的颜色
//_tabBarController.tabBar.barTintColor = [UIColor redColor];
//3.改变tabBar上的视图的渲染颜色
// _tabBarController.tabBar.tintColor = [UIColor redColor];
//4.设置tabBar默人选中item 默认是0
_tabBarController.selectedIndex = 0;
//5.给 tabBar添加背景图片 tabBar的高度是49
_tabBarController.tabBar.backgroundImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"123" ofType:@"jpg"]];
2.给window添加根视图控制器
self.window.rootViewController = _tabBarController;
3.自定义badgevalue,并改变位置和大小,值得注意的是,一定要在创建uitabBarcontroller对象后,再定义角标
UILabel *badgeLabel = [[UILabel alloc]initWithFrame:CGRectMake(270, 4, 16, 16)];
badgeLabel.backgroundColor = [UIColor redColor];
badgeLabel.text = @"5";
badgeLabel.textAlignment = NSTextAlignmentCenter;
badgeLabel.textColor = [UIColor whiteColor];
badgeLabel.font = [UIFont systemFontOfSize:16];
//截取圆角
badgeLabel.layer.cornerRadius = 8;//圆角弯曲程度
badgeLabel.layer.masksToBounds = YES;//是否减掉边角
//放在tabBar上显示
[_tabBarController.tabBar addSubview:badgeLabel];
[badgeLabel release];
4.重写初始化方法
//重写初始化方法
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// //创建firstvc里的标签视图(使用系统自带方法)
// UITabBarItem *firstItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:101];
// //给tabBar上添加标签视图
// self.tabBarItem = firstItem;
//
// //给tabBar上的视图添加角标
firstItem.badgeValue = @"10";
// [firstItem release];
//自定义标签视图
UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTitle:nil image:[[UIImage imageNamed:@"icon01"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"icon01_s"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];//,该方法中第一个参数为tabBar上图片下的文字, 第2个参数为未点击时图片样式,第3个参数为点击时的图片样式
// 添加标签视图
self.tabBarItem = tabBarItem;
[tabBarItem release];
//调整图标的位置 使用uitabbaritem父类中的继承到的属性 imagesInset(上,左,下,右)
tabBarItem.imageInsets = UIEdgeInsetsMake(7, 0, -7, 0);
}
return self;
}
5.一点补充
tabBar中一个很重要的属性是hidesBottomBarWhenPushed,该属性控制该视图在push到其他界面时是否会隐藏tabBar,默认置为NO。
@property(nonatomic) BOOL hidesBottomBarWhenPushed; // If YES, then when this view controller is pushed into a controller hierarchy with a bottom bar (like a tab bar), the bottom bar will slide out. Default is NO.