自定义标签工具栏

本文将介绍如何在iOS应用中自定义底部导航栏的设计,包括隐藏默认工具栏、创建自定义标签工具栏、添加按钮以及实现按钮点击事件等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先创建三级控制器

隐藏默认生成的工具栏

//自定义标签工具栏

- (void)_initTabbarView {


    //(1)隐藏默认生成的工具栏

    self.tabBar.hidden = YES;

    

    //(2)创建视图

    CGFloat height = [UIScreen mainScreen].bounds.size.height;

    CGFloat width = [UIScreen mainScreen].bounds.size.width;

    tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, height-49, width, 49)];

    //设置背景颜色

    tabbarView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"navbg"]];

    [self.view addSubview:tabbarView];

    

    //(3)添加按钮

    

    //每一个按钮的宽度

    CGFloat w =  width/5;

    

    for (int i=1; i<6; i++) {

        //创建按钮

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        button.tag = i-1;

        //设置图片

        NSString *imgName = [NSString stringWithFormat:@"%d",i];

        [button setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];

        //设置frame

        button.frame = CGRectMake((w-42)/2+w*(i-1), 2, 42, 45);

        //添加一个点击事件

        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

        [tabbarView addSubview:button];

    }

    

    //4.创建选中图片

    _selectedImg = [[UIImageView alloc] initWithFrame:CGRectMake((w-53)/2.0, 2, 53, 45)];

    _selectedImg.image = [UIImage imageNamed:@"选中"];

    [tabbarView addSubview:_selectedImg];

    

}


/按钮的点击事件

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


    //切换视图控制器

    self.selectedIndex = button.tag;

    

    //动画

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:.2];

    

    _selectedImg.center = button.center;

    

    [UIView commitAnimations];

    

}

第二种是直接移除工具栏上的子视图


//2.自定义工具栏的方法

- (void)_newInitTabbar {


    //(1)移除工具栏上的按钮

    //取得tabbar上的所有子视图

    NSArray *views = [self.tabBar subviews];

    for (UIView *view in views) {

        [view removeFromSuperview];

    }

    

    //(2)设置背景

    self.tabBar.backgroundImage = [UIImage imageNamed:@"navbg"];

    

    //(3)创建按钮

    CGFloat width = [UIScreen mainScreen].bounds.size.width;

    //每一个按钮的宽度

    CGFloat w =  width/5;

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

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        NSString *imageName = [NSString stringWithFormat:@"%d",i+1];

        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];

        //设置frame

        button.frame = CGRectMake((w-42)/2+w*i, 2, 42, 45);

        //添加一个点击事件

        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

        [self.tabBar addSubview:button];

    }

    

    //(4)创建选中图片

    _selectedImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"选中"]];

    _selectedImg.frame = CGRectMake((w-53)/2.0, 2, 53, 45);

    [self.tabBar addSubview:_selectedImg];

    

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值