UITabBarController/分栏控制器/标签栏控制
注】标签栏控制器,是UIKit框架提供的一个容器视图控制器,用于切换相同级别的兄弟视图控制器。iOS允许将导航控制器添加到标签栏控制器中进行管理(事实上这种结构非常常见),但原则上,不推荐将分栏控制器添加到导航控制器中进行管理。
【注】标签栏的高度 49
vc.hidesBottomBarWhenPushed = YES;
我们的标签栏最多只能放5个Controller
一、UITabBarController的使用
1.使用步骤:
(1)初始化UITabBarController
(2)设置UIWindow的rootViewController为UITabBarController
(3)创建相应的子控制器(viewcontroller)
(4)把子控制器添加到UITabBarController
2.UITabBar
下方的工具条称为UITabBar ,如果UITabBarController有N个子控制器,那么UITabBar内部就会有N 个UITabBarButton作为子控件与之对应。UITabbarButton在UITabBar中的位置是均分的搞都都为49.UITabBar的默认高度,
3.UITabBarButton
UITabBarButton⾥面显⽰什么内容,由对应子控制器的tabBarItem属性来决定
二、UITabBarController的自定义
【注】
1.TabBarViewController本身只有一个,无需定制
2.TabBar属于TabBarViewController,用于显示每个标签,可以定制
3.每个视图控制器拥有属于自己的TabItem(标签),需要定制
a.新建一个ViewController继承 TabBarViewController
b.定制TabBar
//定制TabBar
-(void)customTabBar
{
NSArray *name=@[@"主页",@"收藏",@"位置",@"设置"];//tabBar的title
NSArray *imageName=@[@"tab_0",@"tab_1",@"tab_2",@"tab_3"];//tabBar图标正常显示的图
NSArray *selectImageName=@[@"tab_c0",@"tab_c1",@"tab_c2",@"tab_c3"];//tabBar被选中的图
//定制tabBar的背景图
UIImageView *tabBar_bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-49, self.view.frame.size.width, 49)];
tabBar_bg.image=[UIImage imageNamed:@"tabbg"];
tabBar_bg.userInteractionEnabled=YES;
[self.view addSubview:tabBar_bg];
//
for(int i=0;i<4;i++)
{
UIButton *tabBatButton=[[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width/4*i, 0, self.view.frame.size.height/4, 49)];
[tabBatButton setTitle:name[i] forState:UIControlStateNormal];
[tabBatButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[tabBatButton setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
tabBatButton.titleLabel.font=[UIFont systemFontOfSize:10];
tabBatButton.titleEdgeInsets=UIEdgeInsetsMake(33,-30, 0,55 );
[tabBatButton setImage:[UIImage imageNamed:imageName[i]] forState:UIControlStateNormal];
[tabBatButton setImage:[UIImage imageNamed:selectImageName[i]] forState:UIControlStateSelected];
tabBatButton.imageEdgeInsets=UIEdgeInsetsMake(3,0,16,40);
[tabBar_bg addSubview:tabBatButton];
if(0==i)
{//默认第一个标签被选中
tabBatButton.selected=YES;
tabBatButton.userInteractionEnabled=NO;
[tabBatButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/4*i+20, 47, 40, 2)];
view.backgroundColor=[UIColor orangeColor];
view.tag=100;
[tabBatButton addSubview:view];
}
tabBatButton.tag=10+i;
[tabBatButton addTarget:self action:@selector(OnClick:) forControlEvents:UIControlEventTouchUpInside];
[tabBatButton release];
}
[tabBar_bg release];
}
-(void)OnClick:(UIButton*)button
{
button.selected=YES;//当被选中的时候状态为被选中
button.userInteractionEnabled=NO;
[button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
self.selectedIndex=button.tag-10;
UIView *view=[self.view viewWithTag:100];
//选中的动画
[UIView animateWithDuration:0.3 animations:^{
view.frame=CGRectMake(button.frame.origin.x+20, 47, 40, 2);
} completion:nil];
for(int i=0;i<4;i++)
{
UIButton *btn=(UIButton*)[self.view viewWithTag:i+10];
if(btn.tag==button.tag)
{
continue;
}
btn.userInteractionEnabled=YES;
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
btn.selected=NO;
}
}
效果图: