產(chǎn)品:" tabBar 上要6個(gè)標(biāo)簽欄"
"iOS 官方設(shè)置,最多有5個(gè).而且6個(gè)用戶體驗(yàn)不太好"
"安卓那邊可以,你跟進(jìn)一下"
so,你需要自定義 tabBar
設(shè)置類似于微博的 tabBar
- 自定義 tabBarController ,定義4個(gè)VC,添加到 tabBarController 中
- (void)creatAddVC:(UIViewController *)vc title:(NSString *)title imgName:(NSString *)imgName{
vc.title = title;
vc.tabBarItem.image = [[UIImage imageNamed:imgName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
NSString *selImgName = [NSString stringWithFormat:@"%@_sel",imgName];
vc.tabBarItem.selectedImage = [[UIImage imageNamed:selImgName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
[self addChildViewController:nav];
}
- 自定義一個(gè)繼承自 UITabBar 的 TabBar,通過KVC,將該自定義的 tabBar 設(shè)置為 tabBarController 的 tabBar
// 將代碼放在 viewDidLoad中
UIViewController *vc4 = [UIViewController new];
vc4.view.backgroundColor = [UIColor purpleColor];
[self creatAddVC:vc4 title:@"我" imgName:@"me"];
HSTabBar *customTabBar = [HSTabBar new];
customTabBar.delegate = self;
// KVC
[self setValue:customTabBar forKeyPath:@"tabBar"];
- 在自定義的 tabBar 加入一個(gè)額外的加號按鈕,并對 tabBarItem和加號按鈕重新布局
// layoutsubViews中重新布局,并添加'+'按鈕,
- (void)layoutSubviews{
[super layoutSubviews];
CGFloat width = self.frame.size.width / 5.0;
CGFloat height = self.frame.size.height;
int idx = 0;
for (UIView *subView in self.subviews) {
if (subView.class != NSClassFromString(@"UITabBarButton")) {
continue;
}
if (idx <2) {
subView.frame = CGRectMake(width * idx, 0, width, height);
}else if(idx >= 2){
subView.frame = CGRectMake(width * idx + width, 0, width, height);
}
idx++;
}
UIButton *addBtn = [UIButton new];
[addBtn setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
[addBtn setImage:[UIImage imageNamed:@"add"] forState:UIControlStateHighlighted];
[addBtn addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:addBtn];
addBtn.frame = CGRectMake(width * 2, 0, width, height);
}
// 這里使用代理進(jìn)行點(diǎn)擊回調(diào)
- (void)addClick{
if ([self.delegate respondsToSelector:@selector(click2Add)]) {
[self.delegate click2Add];
}
}
含有6個(gè)標(biāo)簽的 tabBar
用上述的方法確實(shí)可以實(shí)現(xiàn)一些自定義 tabBar 的需求,但是受限與蘋果方法的限制,這還不能顯示標(biāo)簽,無法達(dá)到產(chǎn)品的需求.所以,上述辦法不可用.這里介紹另一個(gè)簡單的方法方法:
- 寫一個(gè)自定義的tabBarController ,添加6個(gè) VC
_vcArr = @[vc1 , vc2, vc3, vc4, vc5, vc6];
for (UIViewController *vc in _vcArr) {
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
[self addChildViewController:nav];
}
self.selectedIndex = 0;
- 定一個(gè)繼承自 UIView 的 tabBarView,將這個(gè) tabBarView 添加到自定義的 tabBarController 上
SSBarView *barView = [[SSBarView alloc]initWithFrame:self.tabBar.bounds];
self.barView = barView;
[self.tabBar addSubview:barView];
barView.delegate = self;
- 在定義的 tabBarView 中,設(shè)置6個(gè)按鈕,按照要求布局好
- (void)creatUI{
self.userInteractionEnabled = YES;
self.backgroundColor = [UIColor orangeColor];
NSArray *arr = @[@"我", @"是", @"中", @"國", @"人", @"民"];
CGFloat btnW = self.frame.size.width / arr.count;
CGFloat btnH = self.frame.size.height;
for (NSString *text in arr) {
NSUInteger idx = [arr indexOfObject:text];
UIButton *btn = [UIButton new];
[self addSubview:btn];
btn.frame = CGRectMake(btnW * idx, 0, btnW, btnH);
btn.tag = tagId + idx;
[btn setTitle:text forState:UIControlStateNormal];
[btn setTitle:text forState:UIControlStateSelected];
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
[self.btnArr addObject:btn];
}
}
- 通過代理或者 block 回調(diào),在點(diǎn)擊某個(gè)按鈕的時(shí)候,tabBarController 切換不同的 selectedIndex
// BarView 的代理
- (void)click2GetIndex:(int)index{
self.selectedIndex = index;
}
具體的請參考:demo