iOS学习笔记 (9) UITabBarController分栏控制器

来源:互联网 发布:java调用db2存储过程 编辑:程序博客网 时间:2024/05/19 22:54

UITabBarController/分栏控制器/标签栏控制

注】标签栏控制器,是UIKit框架提供的一个容器视图控制器,用于切换相同级别的兄弟视图控制器。iOS允许将导航控制器添加到标签栏控制器中进行管理(事实上这种结构非常常见),但原则上,不推荐将分栏控制器添加到导航控制器中进行管理。

【注】标签栏的高度 49

vc.hidesBottomBarWhenPushed =YES;


我们的标签栏最多只能放5个Controller


一、UITabBarController的使用

1.使用步骤:

1)初始化UITabBarController

2)设置UIWindowrootViewControllerUITabBarController

3)创建相应的子控制器(viewcontroller

4)把子控制器添加到UITabBarController



2.UITabBar


下方的工具条称为UITabBar,如果UITabBarControllerN个子控制器,那么UITabBar内部就会有NUITabBarButton作为子控件与之对应。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=@[@"主页",@"收藏",@"位置",@"设置"];//tabBartitle

    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=[[UIImageViewalloc]initWithFrame:CGRectMake(0,self.view.frame.size.height-49,self.view.frame.size.width,49)];

    tabBar_bg.image=[UIImageimageNamed:@"tabbg"];

    tabBar_bg.userInteractionEnabled=YES;

    [self.viewaddSubview:tabBar_bg];

    

//

   for(int i=0;i<4;i++)

    {

        UIButton *tabBatButton=[[UIButtonalloc]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:[UIColorgrayColor] forState:UIControlStateNormal];

        [tabBatButton setTitleColor:[UIColororangeColor] forState:UIControlStateHighlighted];

        tabBatButton.titleLabel.font=[UIFontsystemFontOfSize:10];

        tabBatButton.titleEdgeInsets=UIEdgeInsetsMake(33,-30,0,55 );

        [tabBatButton setImage:[UIImageimageNamed:imageName[i]] forState:UIControlStateNormal];

        [tabBatButtonsetImage:[UIImageimageNamed:selectImageName[i]] forState:UIControlStateSelected];

        tabBatButton.imageEdgeInsets=UIEdgeInsetsMake(3,0,16,40);

        [tabBar_bgaddSubview:tabBatButton];

       if(0==i)

        {//默认第一个标签被选中

            tabBatButton.selected=YES;

            tabBatButton.userInteractionEnabled=NO;

            [tabBatButton setTitleColor:[UIColororangeColor] forState:UIControlStateNormal];

           UIView *view=[[UIViewalloc]initWithFrame:CGRectMake(self.view.frame.size.width/4*i+20,47, 40, 2)];

            view.backgroundColor=[UIColororangeColor];

            view.tag=100;

            [tabBatButtonaddSubview:view];

        }

        tabBatButton.tag=10+i;

        [tabBatButton addTarget:selfaction:@selector(OnClick:)forControlEvents:UIControlEventTouchUpInside];


        [tabBatButtonrelease];

        

    }

    [tabBar_bgrelease];

    

    

}

-(void)OnClick:(UIButton*)button

{

    

    button.selected=YES;//当被选中的时候状态为被选中

    button.userInteractionEnabled=NO;

    [button setTitleColor:[UIColororangeColor] forState:UIControlStateSelected];

    self.selectedIndex=button.tag-10;

   UIView *view=[self.viewviewWithTag:100];

    //选中的动画

    [UIViewanimateWithDuration:0.3animations:^{

    

        view.frame=CGRectMake(button.frame.origin.x+20,47, 40, 2);

    } completion:nil];

   for(int i=0;i<4;i++)

    {

       UIButton *btn=(UIButton*)[self.viewviewWithTag:i+10];

       if(btn.tag==button.tag)

        {

           continue;

        }

        btn.userInteractionEnabled=YES;

        [btn setTitleColor:[UIColorgrayColor] forState:UIControlStateNormal];

        btn.selected=NO;

    }

    

}


效果图:


0 0
原创粉丝点击