#自定義tabbar的使用
##1總體思想
通過(guò)在view上添加自定義的button,再將view添加到tabbar上,這樣就能實(shí)現(xiàn)自定義tabbaritems
##2需求分析
由于各個(gè)tabbaritems的高度,大小不一樣,因此使用系統(tǒng)的tabbar并不能滿足需求,因此需要我們自定義items,,所以選用自定義items來(lái)代替系統(tǒng)tabbaritems.但是由于tabbarController只能添加tabbaritems,所以選擇使用tabbaritems僅僅來(lái)管理各個(gè)viewController,其他的功能,全都交給自定義的items來(lái)實(shí)現(xiàn).
## 3代碼實(shí)現(xiàn)
###1自定義tabbaritems的創(chuàng)建
**首先,**創(chuàng)建一個(gè)結(jié)構(gòu)體,用于區(qū)別是正常大小的tabbar,還是大小不一的tabbaritem
typedef NS_ENUM(NSUInteger, LXYTabBarItemType) {
LXYTabBarItemNormal = 0,
LXYTabBarItemRise,
};
**然后**,通過(guò) `layoutSubviews` 方法來(lái)實(shí)現(xiàn)了image的的textlabel和imageview的大小和位置,
**最后,**通過(guò)重寫`- (void)setHighlighted:(BOOL)highlighted`方法來(lái)禁止按鈕高亮,防止按鈕高亮帶來(lái)的ui變化.
### 2自定義tabbar的創(chuàng)建
####添加的屬性
由于自定義tabbar是view的重寫的,所以我們需要給view添加`tabbaritems`以及`協(xié)議`屬性
#### tabbar背景
由于自定義tabbar是用view重寫的,所以首先需要在初始化的時(shí)候,完成背景的設(shè)置
- (void)config {
//添加背景
self.backgroundColor = [UIColor whiteColor];
UIImageView *topLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, -5, SCREEN_WIDTH, 5)];
topLine.image = [UIImage imageNamed:@"tapbar_top_line"];
[self addSubview:topLine];
}
實(shí)現(xiàn)`config`方法后,再在view的初始化方法中調(diào)用該方法,即可完成view的背景的設(shè)置
#### set方法
- (void)setTabBarItems:(NSArray *)tabBarItems {
_tabBarItems = tabBarItems;
NSInteger itemTag = 0;
for (id item in tabBarItems) {
if ([item isKindOfClass:[LXYTabBarItem class]]) {
if (itemTag == 0) {
((LXYTabBarItem *)item).selected = YES;
}
[((LXYTabBarItem *)item) addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchDown];
[self addSubview:item];
if (((LXYTabBarItem *)item).tabBarItemType != LXYTabBarItemRise) {
((LXYTabBarItem *)item).tag = itemTag;
itemTag++;
}
}
}
}
該方法是tabbaritem的set方法,由于tabbaritems是一組button,因此在添加到view的時(shí)候,確定了選中同時(shí)添加按鈕觸發(fā)的方法 `itemSelected:`,并且標(biāo)記了正常大小的按鈕的`tag`
####按鈕綁定方法itemSelected
- (void)itemSelected:(LXYTabBarItem *)sender {
if (sender.tabBarItemType != LXYTabBarItemRise) {
[self setSelectedIndex:sender.tag];
} else {
if (self.delegate) {
if ([self.delegate respondsToSelector:@selector(tabBarDidSelectedRiseButton)]) {
[self.delegate tabBarDidSelectedRiseButton];
}
}
}
}}
按鈕綁定方法判斷了2種情況.
##### 正常尺寸按鈕
調(diào)用`- (void)setSelectedIndex:(NSInteger)index`方法,
##### 異常尺寸按鈕
異常尺寸按鈕在本demo中就是位于tabbar中間的發(fā)布按鈕.,調(diào)用協(xié)議方法`[self.delegate tabBarDidSelectedRiseButton]`
#### 正常按鈕點(diǎn)擊觸發(fā)方法
- (void)setSelectedIndex:(NSInteger)index
{
for (LXYTabBarItem *item in self.tabBarItems) {
if (item.tag == index) {
item.selected = YES;
} else {
item.selected = NO;
}
}
UIWindow *keyWindow = [[[UIApplication sharedApplication] delegate] window];
UITabBarController *tabBarController = (UITabBarController *)keyWindow.rootViewController;
if (tabBarController) {
tabBarController.selectedIndex = index;
}}
將點(diǎn)擊的按鈕設(shè)置為選擇狀態(tài),并將tabBarController中對(duì)應(yīng)的viewController設(shè)置為選擇狀態(tài)
### 3tabbaritems初始化
- (LXYTabBarItem *)tabBarItemWithFrame:(CGRect)frame title:(NSString *)title normalImageName:(NSString *)normalImageName selectedImageName:(NSString *)selectedImageName tabBarItemType:(LXYTabBarItemType)tabBarItemType {
LXYTabBarItem *item = [[LXYTabBarItem alloc] initWithFrame:frame];
[item setTitle:title forState:UIControlStateNormal];
[item setTitle:title forState:UIControlStateSelected];
item.titleLabel.font = [UIFont systemFontOfSize:8];
UIImage *normalImage = [UIImage imageNamed:normalImageName];
UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
[item setImage:normalImage forState:UIControlStateNormal];
[item setImage:selectedImage forState:UIControlStateSelected];
[item setImage:selectedImage forState:UIControlStateHighlighted];
[item setTitleColor:[UIColor colorWithWhite:51 / 255.0 alpha:1] forState:UIControlStateNormal];
[item setTitleColor:[UIColor colorWithWhite:51 / 255.0 alpha:1] forState:UIControlStateSelected];
item.tabBarItemType = tabBarItemType;
return item;
}
初始化tabbaritems的`title`,`normalImage`,`selectedImage`以及`LXYtabBarItem類型