UI初级第六课  Tab 控制器(标签控制器)——iOS学习连载21

1. 使用自定义方式定义 tabbarItem
   
UITabBarItem *item1 = [[ UITabBarItem alloc ] initWithTabBarSystemItem : UITabBarSystemItemFavorites tag : 1 ];
    item1.
badgeValue = @"item1" ;
    homeVC.
tabBarItem = item1;
   
   
UITabBarItem *item2 = [[ UITabBarItem alloc ] initWithTabBarSystemItem : UITabBarSystemItemBookmarks tag : 2 ];
    item2.
badgeValue = @"haha" ;
    messageVC.tabBarItem = item2;
2. 设置 tabbar 的背景颜色
tabCtrl.tabBar.barTintColor = [UIColor cyanColor];
3.设置选中图片的颜色
    tabCtrl. tabBar . tintColor = [ UIColor cyanColor ];  
4.设置选中 item 后,显示在此 item 下面的图片
tabCtrl.tabBar.selectionIndicatorImage = [UIImageimageNamed:@"选中.png"];
5. 隐藏自己的 tabbarView
self.tabBar.hidden = true;
6. 创建选中视图
   
UIImageView *selectedView = [[ UIImageView alloc ] initWithFrame : CGRectMake ( 0 , 0 , 53 , 45 )];
    selectedView.
tag = 100 ;
    selectedView.image = [UIImageimageNamed:@"选中.png"];
7.创建标签栏的5个按钮
    for ( int i = 0 ; i < 5 ; i++) {
       
UIButton *btn = [ UIButton buttonWithType : UIButtonTypeCustom ];
        btn.
tag = 200 + i;
        btn.
frame = CGRectMake (scrrenWidth / 5 * i, 0 , scrrenWidth / 5 , 49 );
        [btn
addTarget : self action : @selector (btnClick:) forControlEvents : UIControlEventTouchUpInside ];
       
NSString *imgName = [ NSString stringWithFormat : @"%d.png" , i + 1 ];
        [btn
setImage :[ UIImage imageNamed :imgName] forState : UIControlStateNormal ];
        [_tabbarViewaddSubview:btn];
//设置选中图片中心与btn图片中心相同
        if (i == 0 ) {
            selectedView.
center = btn. center ;
        }
    }
- ( void )btnClick:( UIButton *)btn
{
   
   
// 设置选择的控制器
   
self . selectedIndex = btn. tag - 200 ;
   
   
UIView *selectedView = [ _tabbarView viewWithTag : 100 ];
   
   
    [
UIView animateWithDuration : 0.3
                    
animations :^{
                         selectedView.
center = btn. center ;
                     }];
}
8. - ( void )_initViewControllers
{
    // 三级控制器
   
HomeViewController *homeVC = [[ HomeViewController alloc ] init ];
   
MessageViewController *messageVC = [[ MessageViewController alloc ] init ];
   
SearchViewController *searchVC = [[ SearchViewController alloc ] init ];
   
SettingViewController *settingVC = [[ SettingViewController alloc ] init ];
   
MoreViewController *moreVC = [[ MoreViewController alloc ] init ];
   
   
NSArray *viewControllers = @[ homeVC, messageVC, searchVC, settingVC, moreVC ] ;
   
   
NSMutableArray *navs = [ NSMutableArray array ];
   
// 二级控制器
   
for ( int i = 0 ; i < viewControllers. count ; i ++) {
       
UINavigationController *nav = [[ UINavigationController alloc ] initWithRootViewController :viewControllers[i]];
        nav.
delegate = self ;
        [navs
addObject :nav];
    }
   
   
// 一级控制器
   
self . viewControllers = navs;
   
}
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值