iOS 开发 自定义Tabbar

自定义tabbar怎么做,,后来感觉有必要写出来分享一下,


首先创建一个file继承 UITabBarController   在.m文件中添加下面代码  ,,其中FirstViewController    SecondViewController    ThirdViewController

 为tabbar里切换的三个页面,

    NSMutableArray *tabbarElementArray = [[NSMutableArrayalloc]init];

    

    FirstViewController *first = [[FirstViewControlleralloc]init];  

    UINavigationController *firstNavi = [[UINavigationControlleralloc]initWithRootViewController:first];

    

    SecondViewController *second = [[SecondViewControlleralloc]init];

    UINavigationController *secondNavi = [[UINavigationControlleralloc]initWithRootViewController:second];

    

    ThirdViewController *third = [[ThirdViewControlleralloc]init];

    UINavigationController *thirdNavi = [[UINavigationControlleralloc]initWithRootViewController:third];

    

    [tabbarElementArray addObject:firstNavi];

    [tabbarElementArray addObject:secondNavi];

    [tabbarElementArray addObject:thirdNavi];

    

    self.viewControllers = tabbarElementArray;


自定义tabor上面button 的样式和label样式

-(void)creatTabbarButton{

    self.NewTabbar = [[UIViewalloc]initWithFrame:CGRectMake(0, [UIScreenmainScreen].bounds.size.height-49, [UIScreen mainScreen].bounds.size.width,49)];

    self.NewTabbar.backgroundColor = [UIColorwhiteColor];

    [self.viewaddSubview:self.NewTabbar];

    _buttonArray =@[@"fa-reply",@"fa-road",@"fa-rss"];

    buttonImageArray =@[@"chat",@"find",@"me"];

    buttonClickArray =@[@"chatclicked",@"findcilcked",@"meclicked"];

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

        self.tabbarButton = [UIButtonbuttonWithType:UIButtonTypeCustom];

        self.tabbarButton.frame =CGRectMake(([UIScreenmainScreen].bounds.size.width/3-5)*i+20,0,72,50);

        

        [_tabbarButtonsetImage:[UIImageimageNamed:buttonImageArray[i]]forState:UIControlStateNormal];

        [_tabbarButtonsetImage:[UIImageimageNamed:buttonClickArray[i]]forState:UIControlStateSelected];

        [_tabbarButtonaddTarget:selfaction:@selector(ChangeViewController:)forControlEvents:UIControlEventTouchUpInside];

        _tabbarButton.tag =100+i;

        [_NewTabbaraddSubview:_tabbarButton];

        _tabbarLabel = [[UILabelalloc]initWithFrame:CGRectMake(([UIScreenmainScreen].bounds.size.width/3-5)*i,5, [UIScreenmainScreen].bounds.size.width/3,15)];

        _tabbarLabel.tag =200+i;

        _tabbarLabel.text =_buttonArray[i];

        _tabbarLabel.textColor = [UIColorblackColor];

        _tabbarLabel.font = [UIFontsystemFontOfSize:12];

        _tabbarLabel.textAlignment =NSTextAlignmentCenter;

        

        [_NewTabbaraddSubview:_tabbarLabel];//tabbar图标下面的文字,注释

    }

    UIButton *button = (UIButton *)[_NewTabbarviewWithTag:100];

    button.selected = YES;

    UILabel *selectLabel = (UILabel *)[_NewTabbarviewWithTag:200];

    selectLabel.textColor  =[UIColorredColor];

}


点击事件操作

-(void)ChangeViewController:(UIButton *)button{

    self.selectedIndex = button.tag-100;

    button.selected = YES;

    

    self.hidesBottomBarWhenPushed =YES;

    //遍历tabBar上面的所有button

    for (UIView *newBtnin_NewTabbar.subviews) {

        if ([newBtn isKindOfClass:[UIButton class]]) {//判断是否是UIButton的实例对象

            if (newBtn.tag == button.tag) {//如果是被点击的按钮

                //设置为选中状态

                [(UIButton *)newBtn setSelected:YES];

            }else{

                //取消选中

                [(UIButton *)newBtn setSelected:NO];

            }

            

        }else if ([newBtnisKindOfClass:[UILabelclass]]){

            if (newBtn.tag == button.tag+100) {

                ((UILabel *)newBtn).textColor = [UIColorredColor];

            }else{

                ((UILabel *)newBtn).textColor = [UIColorblackColor];

            }

        }

    }

}


以上为自定义tabbar的基本操作,,本处提供demo下载,,https://github.com/yongchangye/DefineTabbar






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值