自定义TabBarItem

本文介绍了如何在iOS应用中自定义TabBarItem,通过删除系统TabBarItem并添加自定义的Item来实现。步骤包括遍历删除系统项,创建并初始化自定义TabBarItem,将其添加到tabBar并设置点击事件。

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

自定义tabBar有好多种方法,本文介绍的是:删除TabBarItem,添加自定义Item

1、 首先遍历删除系统TabBarItem:

    NSArray *systemTabbarItemsArray = self.tabBar.subviews;
    //遍历tabbar中的子视图
    for (UIView *view in systemTabbarItemsArray) {
        Class cls = NSClassFromString(@"UITabBarButton");
        if ([view isKindOfClass:cls]) {
            //移除tabbar上的按钮
            [view removeFromSuperview];
        }
    }

2、接着创建自定义的TabBarItem(如果需要自定义的话),自定义初始化方法

   //自定义初始化方法
- (instancetype)initWithFrame:(CGRect)frame
                    imageName:(NSString *)name
                        title:(NSString *)title
                     fontSize:(NSInteger)fontSize
                    isBarItem:(BOOL)isItem
{
    self = [super initWithFrame:frame];
    if (self) {

        //创建子视图
        //创建图片
        UIImageView *imgView = [[UIImageView alloc] init];

        if (isItem) {
            //tabbar上的
            imgView.frame = CGRectMake((frame.size.width - 20) / 2, 8, 20, 20);
        }else {
            imgView.frame = CGRectMake(0, 0, self.width, self.width);
        }

        imgView.tag = 101;
        //设置内容格式
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.image = [UIImage imageNamed:name];
        [self addSubview:imgView];

        //创建Label
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, imgView.bottom, frame.size.width, 20)];
        titleLabel.tag = 102;
        titleLabel.text = title;
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        //设置字体大小
        titleLabel.font = [UIFont systemFontOfSize:fontSize];
        [self addSubview:titleLabel];

    }

    return self;
} 

3、添加自定义的tabBarItem到系统tabBar,并添加点击事件

    //创建自定义Item
    for (int i = 0; i < menuTitle.count; i++) {

        MenuTabBarItem *menuItem = [[MenuTabBarItem alloc] initWithFrame:CGRectMake(kMenuItemX, kMenuItemY, kMenuItemW, kMenuItemH)
                                                               imageName:menuImgName[i]
                                                                   title:menuTitle[i]
                                                                fontSize:10
                                                               isBarItem:YES];

        //设置tag值
        menuItem.tag = 1000 + i;

        //设置label默认的颜色
        UILabel *menuLabel = (UILabel *)[menuItem viewWithTag:102];
        menuLabel.textColor = kDarGrayColor;

        //添加点击事件
        [menuItem addTarget:self action:@selector(menuItemClickAction:) forControlEvents:UIControlEventTouchUpInside];

        [self.tabBar addSubview:menuItem];

        if (menuItem.tag == 1000) {

            UIImageView *menuItemImgView = (UIImageView *)[menuItem viewWithTag:101];
            UILabel *menuItemLabel = (UILabel *)[menuItem viewWithTag:102];

            menuItemImgView.image = [UIImage imageNamed:menuImgName_Select[0]];
            menuItemLabel.textColor = kItemColor;
        }
    }

4、点击事件触发,切换控制器,恢复上次选择的,设置本次选择的

//tabbar按钮点击触发事件
- (void)menuItemClickAction:(MenuTabBarItem *)menuItem
{
    NSInteger currentIndex = menuItem.tag - 1000;

    //切换控制器
    self.selectedViewController = self.viewControllers[currentIndex];

    //设置当前选中的有颜色
    UIImageView *menuItemImgView = (UIImageView *)[menuItem viewWithTag:101];
    UILabel *menuItemLabel = (UILabel *)[menuItem viewWithTag:102];

    menuItemImgView.image = [UIImage imageNamed:menuImgName_Select[currentIndex]];
    menuItemLabel.textColor = kItemColor;

    //取消上次选择的设置
    if (currentIndex != _lastIndex) {

        //获取上一次选择的按钮
        MenuTabBarItem *item = (MenuTabBarItem *)[self.tabBar viewWithTag:_lastIndex + 1000];

        //获取imgView和Label
        UIImageView *itemImgView = (UIImageView *)[item viewWithTag:101];
        UILabel *itemLabel = (UILabel *)[item viewWithTag:102];

        itemImgView.image = [UIImage imageNamed:menuImgName[_lastIndex]];
        itemLabel.textColor = kDarGrayColor;
    }

    _lastIndex = currentIndex;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值