1.自定義TabBarController
1.自定義繼承于UITabBarController
2.利用KVC 替換系統(tǒng)的tabBar
3. 利用appearance 全局統(tǒng)一設(shè)置UITabBarItem
[self setValue:[[LCTabBar alloc] init] forKeyPath:@"tabBar"];
NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
normalAttrs[NSForegroundColorAttributeName] = [UIColor grayColor]; normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
UITabBarItem *item = [UITabBarItem appearance];
[item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
[item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
2.自定義tabBar
1.自定義繼承于UITabBar
2.初始化
3.布局
2.1中間有線條的tabBar
如圖1

1.png
解決方案: 布局子控件地方, 將中間按鈕 bringSubviewToFront
- (void)layoutSubviews
{
[super layoutSubviews];
CGFloat width = self.width;
CGFloat height = self.height;
self.plusButton.center = CGPointMake(width * 0.5, height * 0.5);
self.plusButton.y = -20;
int index = 0;
CGFloat tabBarButtonW = width / 5;
CGFloat tabBarButtonH = height;
CGFloat tabBarButtonY = 0;
for (UIView *tabBarButton in self.subviews) {
if (![NSStringFromClass(tabBarButton.class) isEqualToString:@"UITabBarButton"]) continue;
CGFloat tabBarButtonX = index * tabBarButtonW;
if (index >= 2) {
tabBarButtonX += tabBarButtonW;
}
tabBarButton.frame = CGRectMake(tabBarButtonX, tabBarButtonY, tabBarButtonW, tabBarButtonH);
index++;
}
[self bringSubviewToFront:self.plusButton];
}
2.2中間按鈕 超出tabBar的范圍不能響應(yīng)點(diǎn)擊事件
如圖 2

2.png
解決方案: 重寫系統(tǒng)的hitTest方法, 不了解的看官可以去查查事件傳遞和響應(yīng)者鏈條
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
if (self.isHidden == NO) { // 當(dāng)前界面 tabBar顯示
CGPoint newPoint = [self convertPoint:point toView:self.plusButton];
if ( [self.plusButton pointInside:newPoint withEvent:event]) { // 點(diǎn) 屬于按鈕范圍
return self.plusButton;
}else{
return [super hitTest:point withEvent:event];
}
}
else {
return [super hitTest:point withEvent:event];
}
}