UITabBar



 UITabBar

1.自定义tabbar过程及注意:

1.新建一个tabBarViewController继承于UITabBarViewController.

2. appdelegate中将tabBarViewController的对象作为根。

MyTabBarViewController *tabBar =[[MyTabBarViewController alloc]init];

    self.window.rootViewController = tabBar ;

    [tabBar release];

3. tabBarViewController中创建tabbar和实例化VC

  主要就是一个UIImageView再加上循环码button,贴在UIImageView上。

btn过程:

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

   {

       UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

       btn.frame = CGRectMake(40+i*(40+50),0 ,40 , 40);

       [btn setImage:[UIImage imageNamed:grayArr[i]]forState:UIControlStateNormal];

       [btn setImage:[UIImage imageNamed:lightArr[i]]forState:UIControlStateSelected];

       [btn addTarget:self action:@selector(btnDown:)forControlEvents:UIControlEventTouchUpInside];

       btn.tag = 1000+i;

       [imgView addSubview:btn];

       

       UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(49+i*90, 37,40, 10)];

       label.text = labArr[i];

       label.font = [UIFont systemFontOfSize:11];

       [imgView addSubview:label];

       label.tag = 2000+i;

      

       

       if(i ==0)

       {

            btn.selected = YES;

            label.textColor = [UIColororangeColor];

       }

        [label release];

       

   }

然后再btnDown中实现对button上图片颜色和字体颜色点击后变化的控制。

-(void)btnDown:(UIButton *)btn

{

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

   {

   UIButton *tempBtn = (UIButton *)[self.view viewWithTag:1000+i];

       UILabel *tempLab = (UILabel *)[self.view viewWithTag:2000+i];

       

   if(tempBtn == btn)

    {

      tempBtn.selected = YES;

        tempLab.textColor = [UIColor orangeColor];

    }

    else

    {

       tempBtn.selected = NO;

        tempLab.textColor = [UIColor colorWithRed:149/255.0 green:149/255.0blue:149/255.0 alpha:1];

    }

   }

 

   self.selectedIndex= btn.tag-1000;//显示对应的VC

}

特别注意,循环完后一定要写self.selectedIndex = btn.tag-1000;//显示对应的VC。。不然VC视图无法切换。

 

 

创建视图过程:

1

ShouYeViewController *shouye = [[ShouYeViewControlleralloc]init];

   PinDaoViewController *pindao = [[PinDaoViewController alloc]init];

   GeRenViewController *geren = [[GeRenViewController alloc]init];

 2

   UINavigationController *oneNav = [[UINavigationControlleralloc]initWithRootViewController:shouye];

   UINavigationController *twoNav = [[UINavigationControlleralloc]initWithRootViewController:pindao];

   UINavigationController *threeNav = [[UINavigationControlleralloc]initWithRootViewController:geren];

   [shouye release];

   [pindao release];

   [geren release];

 

3

   NSArray *arr = @[oneNav,twoNav,threeNav];

   

   [oneNav release];

   [twoNav release];

   [threeNav release];

4   

   self.viewControllers = arr;

 源码参看:点击打开链接

 练习项目参看:点击打开链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值