首先创建三级控制器
隐藏默认生成的工具栏
//自定义标签工具栏
- (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];
}