要自定義一個(gè)tabbarController那肯定要先繼承UITabBarController了,然后在tabbarController中設(shè)置子控制器和橙。
設(shè)置子控制器需要以下幾個(gè)步驟:
1. 設(shè)置子控制器的文字
childVc.title = title;// 同時(shí)設(shè)置tabbar和navigationBar的文字
2. 設(shè)置子控制器的圖片
childVc.tabBarItem.image = [UIImage imageNamed:image];
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//UIImageRenderingModeAlwaysOriginal 這個(gè)是總是用原圖片忧设,如果不設(shè)置這個(gè)的話(huà)tabbar默認(rèn)都是藍(lán)色的
3. 設(shè)置文字的樣式
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
selectTextAttrs[NSForegroundColorAttributeName] = [UIColor redColor];
[childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
3.1 先給外面?zhèn)鬟M(jìn)來(lái)的小控制器 包裝 一個(gè)導(dǎo)航控制器 (也可以不設(shè)置狂男,看自己心情)
FFNavigationController *nav = [[FFNavigationController alloc] initWithRootViewController:childVc];
4. 添加為子控制器
[self addChildViewController:nav];
以上都做完了可以通過(guò)kvc自定義一個(gè)自己的tabbar,要設(shè)置自己的tabbar只能通過(guò)kvc來(lái)設(shè)置,因?yàn)閁ITabBarController的tabbar是readonly的,不能直接設(shè)置
//UITabBarController的屬性
@property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0); // Provided for -[UIActionSheet showFromTabBar:]. Attempting to modify the contents of the tab bar directly will throw an exception.
FFTabBar *tabBar = [[FFTabBar alloc] init];
tabBar.addbtnDelegate = self;
/** KVC */
[self setValue:tabBar forKey:@"tabBar"];
tabbarController基本就這樣了煮仇,現(xiàn)在看看tabBar里是怎樣的
?在- (void)layoutSubviews給tabbar重新布局
先添加中間大按鈕 (這個(gè)按鈕可以換成自己喜歡的樣子,這邊只是隨便搞了下,大概有個(gè)樣子而已)
UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
addBtn.backgroundColor = [UIColor redColor];
[addBtn addTarget:self action:@selector(addClickAction) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:addBtn];
self.addBtn = addBtn;
CGFloat addBtnViewWidth = self.frame.size.width / count;
CGFloat addBtnViewHeight = self.frame.size.height + 50;
CGFloat addBtnViewX = (self.frame.size.width - addBtnViewWidth) * 0.5;
CGFloat addBtnViewY = -50;
addBtn.frame = CGRectMake(addBtnViewX, addBtnViewY, addBtnViewWidth, addBtnViewHeight);
確定了中間的大按鈕之后谎仲,然后旁邊的UITabBarButton的位置需要重新設(shè)置一下
NSUInteger idx = 0;
for (UIView *childView in self.subviews) {
Class class = NSClassFromString(@"UITabBarButton");
if ([childView isKindOfClass:class]){
if (idx == count / 2) idx++;
CGFloat cvX = idx * self.frame.size.width / count;//count是表示tabbar上一共有幾個(gè)按鈕
childView.frame = CGRectMake(cvX, 0, self.frame.size.width / count, self.frame.size.height);
idx++;
}
}
因?yàn)榇骲utton搞了超過(guò)tabbar的高度了所以要重寫(xiě)一下hitTest方法浙垫,這個(gè)方法可以超出父控件的范圍仍然響應(yīng)點(diǎn)擊事件
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView * view = [super hitTest:point withEvent:event];
if (view == nil) {
// 轉(zhuǎn)換坐標(biāo)系
CGPoint newPoint = [self.addBtn convertPoint:point fromView:self];
// 判斷觸摸點(diǎn)是否在button上
if (CGRectContainsPoint(self.addBtn.bounds, newPoint)) {
view = self.addBtn;
}
}
return view;
}
這樣tabbar上的item就基本設(shè)置完成了,再做個(gè)代理把中間按鈕的點(diǎn)擊事件放開(kāi)出去就基本完成了