自定义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;
}