自定义tabbar怎么做,,后来感觉有必要写出来分享一下,
首先创建一个file继承 UITabBarController 在.m文件中添加下面代码 ,,其中FirstViewController SecondViewController ThirdViewController
为tabbar里切换的三个页面,
NSMutableArray *tabbarElementArray = [[NSMutableArrayalloc]init];
FirstViewController *first = [[FirstViewControlleralloc]init];
UINavigationController *firstNavi = [[UINavigationControlleralloc]initWithRootViewController:first];
SecondViewController *second = [[SecondViewControlleralloc]init];
UINavigationController *secondNavi = [[UINavigationControlleralloc]initWithRootViewController:second];
ThirdViewController *third = [[ThirdViewControlleralloc]init];
UINavigationController *thirdNavi = [[UINavigationControlleralloc]initWithRootViewController:third];
[tabbarElementArray addObject:firstNavi];
[tabbarElementArray addObject:secondNavi];
[tabbarElementArray addObject:thirdNavi];
self.viewControllers = tabbarElementArray;
自定义tabor上面button 的样式和label样式
-(void)creatTabbarButton{
self.NewTabbar = [[UIViewalloc]initWithFrame:CGRectMake(0, [UIScreenmainScreen].bounds.size.height-49, [UIScreen mainScreen].bounds.size.width,49)];
self.NewTabbar.backgroundColor = [UIColorwhiteColor];
[self.viewaddSubview:self.NewTabbar];
_buttonArray =@[@"fa-reply",@"fa-road",@"fa-rss"];
buttonImageArray =@[@"chat",@"find",@"me"];
buttonClickArray =@[@"chatclicked",@"findcilcked",@"meclicked"];
for (int i =0; i<3; i++) {
self.tabbarButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
self.tabbarButton.frame =CGRectMake(([UIScreenmainScreen].bounds.size.width/3-5)*i+20,0,72,50);
[_tabbarButtonsetImage:[UIImageimageNamed:buttonImageArray[i]]forState:UIControlStateNormal];
[_tabbarButtonsetImage:[UIImageimageNamed:buttonClickArray[i]]forState:UIControlStateSelected];
[_tabbarButtonaddTarget:selfaction:@selector(ChangeViewController:)forControlEvents:UIControlEventTouchUpInside];
_tabbarButton.tag =100+i;
[_NewTabbaraddSubview:_tabbarButton];
_tabbarLabel = [[UILabelalloc]initWithFrame:CGRectMake(([UIScreenmainScreen].bounds.size.width/3-5)*i,5, [UIScreenmainScreen].bounds.size.width/3,15)];
_tabbarLabel.tag =200+i;
_tabbarLabel.text =_buttonArray[i];
_tabbarLabel.textColor = [UIColorblackColor];
_tabbarLabel.font = [UIFontsystemFontOfSize:12];
_tabbarLabel.textAlignment =NSTextAlignmentCenter;
[_NewTabbaraddSubview:_tabbarLabel];//tabbar图标下面的文字,注释
}
UIButton *button = (UIButton *)[_NewTabbarviewWithTag:100];
button.selected = YES;
UILabel *selectLabel = (UILabel *)[_NewTabbarviewWithTag:200];
selectLabel.textColor =[UIColorredColor];
}
点击事件操作
-(void)ChangeViewController:(UIButton *)button{
self.selectedIndex = button.tag-100;
button.selected = YES;
self.hidesBottomBarWhenPushed =YES;
//遍历tabBar上面的所有button
for (UIView *newBtnin_NewTabbar.subviews) {
if ([newBtn isKindOfClass:[UIButton class]]) {//判断是否是UIButton的实例对象
if (newBtn.tag == button.tag) {//如果是被点击的按钮
//设置为选中状态
[(UIButton *)newBtn setSelected:YES];
}else{
//取消选中
[(UIButton *)newBtn setSelected:NO];
}
}else if ([newBtnisKindOfClass:[UILabelclass]]){
if (newBtn.tag == button.tag+100) {
((UILabel *)newBtn).textColor = [UIColorredColor];
}else{
((UILabel *)newBtn).textColor = [UIColorblackColor];
}
}
}
}
以上为自定义tabbar的基本操作,,本处提供demo下载,,https://github.com/yongchangye/DefineTabbar