通常在写项目的时候很少直接使用系统的tabbar,因为隐藏或者修改显示风格很麻烦,所以会使用自定义的。
在tabbar.h中添加代码
#import <UIKit/UIKit.h>
@interface MyTabBar : UITabBarController
@property (strong, nonatomic) UIView *tabBarView;
- (void)setTabWithArray:(NSArray *)tabArray NormalImageArray:(NSArray *)normalArray SelectedImageArray:(NSArray *)selectedArray;
@end
在tabbar.m中添加代码
#import "MyTabBar.h"
@interface MyTabBar ()
@end
@implementation MyTabBar
{
NSInteger count;
NSMutableArray *btnArray;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hideTabBar:) name:@"hideTabbar" object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(appearTabBar:) name:@"appearTabbar" object:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
btnArray = [[NSMutableArray alloc]initWithCapacity:0];
[self hideOriginalTab];
self.tabBarView = [[UIView alloc]init];
self.tabBarView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-40, 320, 40);
self.tabBarView.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.tabBarView];
}
- (void)backBtnClick:(NSNotification *)notif
{
[self.navigationController popViewControllerAnimated:YES];
}
//隐藏tabbar
- (void)hideTabBar:(NSNotification *)notif
{
[UIView animateWithDuration:0.2
delay:0.00
options:UIViewAnimationOptionTransitionCurlUp animations:^(void){
self.tabBarView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height, 320, 40);
}completion:nil];
}
//显示tabbar
- (void)appearTabBar:(NSNotification *)notif
{
[UIView animateWithDuration:0.2
delay:0.00
options:UIViewAnimationOptionTransitionCurlUp animations:^(void){
self.tabBarView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-40, 320, 40);
}completion:nil];
}
//给tabbar自定义按钮或其他控件
- (void)setTabWithArray:(NSArray *)tabArray NormalImageArray:(NSArray *)normalArray SelectedImageArray:(NSArray *)selectedArray{
self.viewControllers = tabArray;
count = [tabArray count];
if (tabArray.count > 0) {
for (int i = 0; i < [tabArray count]; i ++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setBackgroundImage:[UIImage imageNamed:[selectedArray objectAtIndex:i]] forState:UIControlStateSelected];
[btn setBackgroundImage:[UIImage imageNamed:[normalArray objectAtIndex:i]] forState:UIControlStateNormal];
btn.tag = i ;
if (btn.tag == 0)
btn.selected = YES;
else
btn.selected = NO;
btn.frame = CGRectMake(320/[tabArray count]*i, 0, 320/[tabArray count], 40);
[btn addTarget:self action:@selector(selectTab:) forControlEvents:UIControlEventTouchUpInside];
[self.tabBarView addSubview:btn];
[btnArray addObject:btn];
}
}
}
- (void)selectTab:(UIButton *)selectBtn{
if(selectBtn.selected == NO)
{
NSInteger selectTag = selectBtn.tag;
selectBtn.selected = YES;
UIViewController *selectVC = [self.viewControllers objectAtIndex:selectTag];
self.selectedViewController = selectVC;
for(int i = 0; i < count; i++)
{
UIButton *btn = (UIButton *)[btnArray objectAtIndex:i];
if (btn.tag != selectTag)
btn.selected = NO;
else
btn.selected = YES;
}
}
}
- (void)hideOriginalTab
{
NSArray *array = [self.view subviews];
UIView *originalTabView = [array objectAtIndex:1];
originalTabView.frame = CGRectMake(0,[UIScreen mainScreen].bounds.size.height, 320, 40);
originalTabView.backgroundColor = [UIColor clearColor];
UIView *newTabView = [array objectAtIndex:0];
newTabView.frame = CGRectMake(0, 0, 320,[UIScreen mainScreen].bounds.size.height);
newTabView.backgroundColor = [UIColor redColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
在使用的地方根据需要添加代码
OneVC *one = [[OneVC alloc]init];
UINavigationController *nav1 = [[UINavigationController alloc]initWithRootViewController:one];
TwoVC *two = [[TwoVC alloc]init];
UINavigationController *nav2 = [[UINavigationController alloc]initWithRootViewController:two];
ThreeVC *three = [[ThreeVC alloc]init];
UINavigationController *nav3 = [[UINavigationController alloc]initWithRootViewController:three];
FourVC *four = [[FourVC alloc]init];
UINavigationController *nav4 = [[UINavigationController alloc]initWithRootViewController:four];
NSArray *controllerArray = [NSArray arrayWithObjects:nav1, nav2, nav3, nav4 ,nil];
NSArray *NormalImageArray = [NSArray arrayWithObjects:@"lishidingdan@2x",@"quedingdingdan@2x",@"wanjiedingdan@2x",@"zaitudingdan@2x", nil];
NSArray *SelectedImageArray = [NSArray arrayWithObjects:@"lishidingdan_anniu@2x",@"quedingdingdan_anniu@2x",@"wanjiedingdan_anniu@2x",@"zaitudingdan_anniu@2x", nil];
MyTabBar *tab = [[MyTabBar alloc]init];
[tab setTabWithArray:controllerArray NormalImageArray:NormalImageArray SelectedImageArray:SelectedImageArray];
self.window.rootViewController = tab;
DEMO下载地址:http://download.youkuaiyun.com/detail/u011918080/6958131