(一)思路:(1)在原来的tabBar空间上面添加一个自定义的tabBar,这样UIViewController的hidesBottomBarWhenPushed属性依然有效;
(2)在自定义的添加UIButton,并加上相应的事件即可;
(3)本实例参考网络上的列子,根据自己项目的要求修改了部分。
(二)自定义tabBar;
#import "XNTabBar.h"
#import "XNTabBarButton.h"
#define kunSelectColor [UIColor clearColor]
#define kSelectColor [UIColor colorWithRed:17.0f/255 green:110.0f/255 blue:181.0f/255 alpha:1.0f]
@interface XNTabBar ()
{
UIButton *clickButton;
XNTabBarButton *barButton;
}
@property (nonatomic,strong) UIButton *selectedBtn;
@end
@implementation XNTabBar
- (void)addButtonWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage barButtonTitle:(NSString *)title btnRect:(CGRect)frame
{
barButton = [[XNTabBarButton alloc] initTabBarButtonWithImage:image tabBarButtonTitle:title tabBarRect:frame];
[barButton addTarget:self action:@selector(btnSelectClick:) forControlEvents:UIControlEventTouchUpInside];//带参数的监听方法记得加"冒号"
[self addSubview:barButton];
}
/**专门用来布局子视图, 别忘了调用super方法*/
- (void)layoutSubviews{
[super layoutSubviews];
[self selectedButtonIndex:self.seletedIndex];
NSInteger count = self.subviews.count;
for (int i = 0; i < count; i++)
{
//取得按钮
UIButton *btnCurrentClick = [self.subviews objectAtIndex:i];
btnCurrentClick.tag = i; //设置按钮的标记, 方便来索引当前的按钮,并跳转到相应的视图
}
}
- (void)selectedButtonIndex:(NSInteger)selectIndex
{
NSInteger count = self.subviews.count;
for (int i = 0; i < count; i++)
{
if (self.seletedIndex == i)
{
UIButton *seleBtn = [self.subviews objectAtIndex:i];
[seleBtn setBackgroundColor:kSelectColor];
[self btnSelectClick:seleBtn];
return;
}
}
}
/**
* 自定义TabBar的按钮点击事件
*/
- (void)btnSelectClick:(UIButton *)button
{
//1.先将之前选中的按钮设置为未选中
self.selectedBtn.selected = NO;
if (self.selectedBtn) {
[self.selectedBtn setBackgroundColor:kunSelectColor];
}
//2.再将当前按钮设置为选中
button.selected = YES;
//却换视图控制器的事情,应该交给controller来做
//最好这样写, 先判断该代理方法是否实现
if ([self.delegate respondsToSelector:@selector(tabBar:selectedFrom:to:)])
{
[self.delegate tabBar:self selectedFrom:self.selectedBtn.tag to:button.tag];
//3.最后把当前按钮赋值为之前选中的按钮
self.selectedBtn = button;
[button setBackgroundColor:kSelectColor];
}
}
(三)自定义tabBar上的按钮;
#import "XNTabBarButton.h"
#define kLblTopOffsety 4.0f
#define kigWidth 20.0f
#define kigHeight 20.0f
#define kLblHeight 15.0f
#define kbgColor [UIColor colorWithRed:0 green:160.0f/255 blue:223.0f/255 alpha:1.0f]
@interface XNTabBarButton ()
{
UIImageView *igBarIcon;
UILabel *lblTitle;
}
@end
@implementation XNTabBarButton
- (instancetype)initTabBarButtonWithImage:(UIImage *)image tabBarButtonTitle:(NSString *)title tabBarRect:(CGRect)rect
{
self = [super initWithFrame:rect];
if (self)
{
CGFloat kbtnTopOffsety = (CGRectGetHeight(rect)-(kigHeight+kLblHeight+kLblTopOffsety))/2;
igBarIcon = [[UIImageView alloc] initWithFrame:({
CGRect frame = CGRectMake((CGRectGetWidth(rect)-kigWidth)/2, kbtnTopOffsety, kigWidth, kigHeight);
frame;
})];
igBarIcon.image = image;
lblTitle = [[UILabel alloc] initWithFrame:({
CGRect frame = CGRectMake(0, CGRectGetMaxY(igBarIcon.frame)+kLblTopOffsety, CGRectGetWidth(rect), kLblHeight);
frame;
})];
lblTitle.text = title;
lblTitle.textColor = [UIColor whiteColor];
lblTitle.textAlignment = NSTextAlignmentCenter;
lblTitle.font = [UIFont systemFontOfSize:12.0f];
[self addSubview:igBarIcon];
[self addSubview:lblTitle];
}
return self;
}
@end
(四)使用;
//删除现有的tabBar
CGRect barRect = self.tabBar.bounds; //这里要用bounds来加, 否则会加到下面去.看不见
if (!self.xnTabBar)
{
self.xnTabBar = [[XNTabBar alloc] init]; //设置代理必须改掉前面的类型,不能用UIView
self.xnTabBar.seletedIndex = self.selectedIndex;
self.xnTabBar.delegate = self; //设置代理
self.xnTabBar.frame = barRect;
self.xnTabBar.backgroundColor = kbgColor;
[self.tabBar addSubview:self.xnTabBar];//添加到系统自带的tabBar上, 这样可以用的的事件方法. 而不必自己去写
//为控制器添加按钮
NSInteger count = self.viewControllers.count;
for (int i = 0; i < count; i++)
{
//根据有多少个子视图控制器来进行添加按钮
NSString *imageName = [NSString stringWithFormat:@"%@",[igArray objectAtIndex:i]];
NSString *barButtonTitle = [NSString stringWithFormat:@"%@",[titleArray objectAtIndex:i]];
UIImage *image = [UIImage imageNamed:imageName];
CGFloat x = i * ceilf(CGRectGetWidth(barRect)/count);
CGFloat y = 0;
CGFloat width = ceilf(CGRectGetWidth(barRect)/count);
CGFloat height = CGRectGetHeight(barRect);
[self.xnTabBar addButtonWithImage:image selectedImage:nil barButtonTitle:barButtonTitle btnRect:CGRectMake(x, y, width, height)];
}
}