首先创建UINavigationController的基类HBNavigationController
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
if (self.childViewControllers.count==1) {
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
- 实现此方法目的: 当你点击界面进入二级子控制器的时候会自动隐藏tabbar
创建tabbarController基类HBTabbarController
- 实现方法,通过kvc方式将系统的tabbar替换为自己自定义的tabbar
[self setValue:hbTabBar forKey:@"tabBar"];
- (void)viewDidLoad {
-
[super viewDidLoad];
HBTabBar * hbTabBar = [[HBTabBar alloc] initWithFrame:KFrame(0, KScreen_Height - 64, KScreen_Width, 64)];
[self setValue:hbTabBar forKey:@"tabBar"];
}
创建tabBar子类HBTabBar继承UITabBar
-(instancetype)initWithFrame:(CGRect)frame{
if([super initWithFrame:frame]){
UIView * tabBar = [[UIView alloc] init];
tabBar.frame = self.bounds;
_tabBar = tabBar;
[self addSubview:tabBar];
}
return self;
}
- tabBar初始化时会调用系统的方法
- (void)setItems:(nullable NSArray<UITabBarItem *> *)items animated:(BOOL)animated
- 重写此方法 == 添加对应的tabBarButtonItems到自定义的tabbar中
- (void)setItems:(nullable NSArray<UITabBarItem *> *)items animated:(BOOL)animated{
[self setLablesWithArrOfTitle:_arrTitle andLeftDistance:_lbl_leftDis andItHeight:_lbl_height andYcoordinate:_Lbl_YCoordinate andDestiView:self];
[self setButtonsWithWidth:_btn_Width andHeiht:_btn_Height andYCoordinate:_Btn_YCoordinate andDestView:self];
}
-(void)setButtonsWithWidth:(CGFloat)width andHeiht:(CGFloat)height andYCoordinate:(CGFloat) y andDestView:(UIView *)destiView{
NSInteger count = _itemsCount;
CGFloat lDis = (0.5*_lbl_width + _lbl_leftDis - _btn_Width * 0.5);
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat margin = (screenWidth - 2*lDis - width * count) / (count - 1);
UIButton * tempBtn = [UIButton new];
for (int i = 0; i< count; i++) {
if (i == 0) {
UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(lDis, y, width, height)];
self.tempBtn = btn;
[btn setBackgroundImage:self.btnImageSelected[i] forState:UIControlStateNormal];
btn.tag = i;
[destiView addSubview:btn];
tempBtn = btn;
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
continue;
}
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake((CGRectGetMaxX(tempBtn.frame)+margin), y, width, height);
[btn setBackgroundImage:self.btnImageNormal[i] forState:UIControlStateNormal];
[destiView addSubview:btn];
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = i;
tempBtn = btn;
}
}
-(void)setLablesWithArrOfTitle:(NSArray<NSString *> *)arrTitles andLeftDistance:(CGFloat)lDistance andItHeight:(CGFloat)height andYcoordinate:(CGFloat) y andDestiView:(UIView *)destiView{
_itemsCount = arrTitles.count;
CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
CGFloat margin = 2 * lDistance;
CGFloat width = (screenWidth - 2 * lDistance - 2 * lDistance * (arrTitles.count - 1))/arrTitles.count;
_lbl_width = width;
UILabel * tempLabel = [UILabel new];
for (int i = 0; i< arrTitles.count; i++) {
if (i == 0) {
UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake(lDistance, y, width, height)];
lbl.backgroundColor = [UIColor blueColor];
self.tempLbl = lbl;
lbl.textColor = self.lblColorSelected;
[self.arrLabel addObject:lbl];
lbl.font = [UIFont systemFontOfSize:_lbl_font];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.text = arrTitles[0];
[destiView addSubview:lbl];
tempLabel = lbl;
continue;
}
UILabel * lbl = [[UILabel alloc] initWithFrame:CGRectMake((CGRectGetMaxX(tempLabel.frame)+margin), y, width, height)];
lbl.backgroundColor = [UIColor blueColor];
lbl.textAlignment = NSTextAlignmentCenter;
lbl.text = arrTitles[i];
lbl.textColor = self.lblColorNormal;
[self.arrLabel addObject:lbl];
lbl.font = [UIFont systemFontOfSize:_lbl_font];
[destiView addSubview:lbl];
tempLabel = lbl;
}
}
- 在.h文件中添加tabbar所有的属性,以便于修改定义tabbar各种属性
@property(nonatomic,copy) void(^callBack)(NSInteger index);
@property (nonatomic, assign) NSInteger lbl_font;
@property (nonatomic, assign) CGFloat lbl_leftDis;
@property (nonatomic, assign) CGFloat lbl_height;
@property (nonatomic, assign) CGFloat Lbl_YCoordinate;
@property (nonatomic, assign) CGFloat btn_Width;
@property (nonatomic, assign) CGFloat btn_Height;
@property (nonatomic, assign) CGFloat Btn_YCoordinate;
@property (strong, nonatomic) NSArray * btnImageNormal;
@property (strong, nonatomic) NSArray * btnImageSelected;
@property (strong, nonatomic) UIColor * lblColorNormal;
@property (strong, nonatomic) UIColor * lblColorSelected;
@property (strong, nonatomic) NSArray * arrTitle;
@property (strong, nonatomic) UIColor * tabBarBackgroundColor;
代码地址 : https://github.com/adampei/diyTabBar