#自定义tabbar的使用
##1总体思想
通过在view上添加自定义的button,再将view添加到tabbar上,这样就能实现自定义tabbaritems
##2需求分析
由于各个tabbaritems的高度,大小不一样,因此使用系统的tabbar并不能满足需求,因此需要我们自定义items,,所以选用自定义items来代替系统tabbaritems.但是由于tabbarController只能添加tabbaritems,所以选择使用tabbaritems仅仅来管理各个viewController,其他的功能,全都交给自定义的items来实现.
## 3代码实现
###1自定义tabbaritems的创建
**首先,**创建一个结构体,用于区别是正常大小的tabbar,还是大小不一的tabbaritem
typedef NS_ENUM(NSUInteger, LXYTabBarItemType) {
LXYTabBarItemNormal = 0,
LXYTabBarItemRise,
};
**然后**,通过 `layoutSubviews` 方法来实现了image的的textlabel和imageview的大小和位置,
**最后,**通过重写`- (void)setHighlighted:(BOOL)highlighted`方法来禁止按钮高亮,防止按钮高亮带来的ui变化.
### 2自定义tabbar的创建
####添加的属性
由于自定义tabbar是view的重写的,所以我们需要给view添加`tabbaritems`以及`协议`属性
#### tabbar背景
由于自定义tabbar是用view重写的,所以首先需要在初始化的时候,完成背景的设置
- (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];
}
实现`config`方法后,再在view的初始化方法中调用该方法,即可完成view的背景的设置
#### 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的时候,确定了选中同时添加按钮触发的方法 `itemSelected:`,并且标记了正常大小的按钮的`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种情况.
##### 正常尺寸按钮
调用`- (void)setSelectedIndex:(NSInteger)index`方法,
##### 异常尺寸按钮
异常尺寸按钮在本demo中就是位于tabbar中间的发布按钮.,调用协议方法`[self.delegate tabBarDidSelectedRiseButton]`
#### 正常按钮点击触发方法
- (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;
}}
将点击的按钮设置为选择状态,并将tabBarController中对应的viewController设置为选择状态
### 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类型