自定义tabbar

电商类的项目像某宝某东的app 都有一个tabbar的平铺导航模式


根据一些demo中案例,个人觉得比较简单易用的自定义的tabbar


1.自定义一个CMTabbarController

#import <UIKit/UIKit.h>

@interface CMMainTabbarController : UITabBarController


在.m文件中把自带的tabbar隐藏掉

self.tabBar.hidden = YES;

2.创建视图控制器

-(void)setController
{
    //建议3-5个子视图为宜
    ViewController1 *vc1 = [[ViewController1 alloc] init];
    //通过此方法把视图加到子视图控制器中
    [self addChildViewController:vc1 title:@""];
}
-(void)addChildViewController:(UIViewController *)childController title:(NSString *)title
{
    //根据传递来的title,判断是否每个字界面都需要加导航栏,
    if ([title isEqualToString:@""]) {
        [self addChildViewController:childController];
    }else{
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:childController];
        childController.navigationItem.title = title;
        //  添加导航栏的背景颜色
        [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"sy2@2x"] forBarMetrics:UIBarMetricsDefault];
        [self addChildViewController:nav];
    }
}
3.创建tabbar按钮

-(void)creatBar:(NSInteger)controllerNum
{
    //图片barbtm按钮图片集
    NSArray *bar_normal = @[];
    NSArray *bar_selected = @[];
    
    //声明一个bar区域
    UIImageView *tabbarView = [[UIImageView alloc] initWithFrame:self.tabBar.frame];
    tabbarView.user<span style="font-family: Arial, Helvetica, sans-serif;">InteractionEnabled = YES;//十分必要</span>

    for (int i = 0; i<self.viewControllers.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(EACH_W(self.viewControllers.count)*i, 0, EACH_W(self.viewControllers.count), EACH_H);
        [btn setImage:[UIImage imageNamed:bar_normal[i]] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:bar_selected[i]] forState:UIControlStateSelected];
        
        //对btn识别
        btn.tag = i+1000;
        
        [tabbarView addSubview:btn];
        
        //设置显示的子视图,默认为首个添加进去的子视图
        if (btn.tag == 1000) {
            [self btnSelected:btn];
        }
        [btn addTagret:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self.view addSubview:tabbarView];
    
}
-(void)btnSelected:(UIButton *)sender
{
    NSLog(@"被点了");
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.selected =NO ;
    sender.selected = YES;
    button = sender;
    self.selectedIndex = sender.tag-1000;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值