IOS之自定义tabbar

我们之前实现利用Tabbar实现的分类管理,但是下面的图片的颜色和背景都不可以改变。我们虽然可以通过一些私有api进行改变,但是上不了app store。下面我自己完全自定义的一个Tabbar。实现的效果图:

image

 

实现的主要代码:

 

 

#pragma mark – View lifecycle 
- (void)loadView 

    [super loadView]; 
    //添加底层背景色 
    [self.view setFrame:CGRectMake(0, 0, 320, 460)]; 
    contentView=[[[UIView alloc] initWithFrame:CGRectMake(0, -20, 320, 460-48)]autorelease]; 
    [self.view addSubview:contentView]; 
    
    UIView *tabBarView=[[[UIView alloc] initWithFrame:CGRectMake(0, 460-48, 320, 48)] autorelease]; 
    [self.view addSubview:tabBarView]; 
    UIImageView *backGroundView=[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBar_background.png"]] autorelease]; 
    [tabBarView addSubview:backGroundView]; 
    
    //添加tabbar 
    TapTabBarItemView *tapTabBarItemView=[[[TapTabBarItemView alloc] init] autorelease]; 
    [tapTabBarItemView setTitle:@"主页" imageName:@"tabBar_1.png" tag:0]; 
    [tabBarView addSubview:tapTabBarItemView]; 
    
    TapTabBarItemView *tapTabBarItemView1=[[[TapTabBarItemView alloc] init] autorelease]; 
    [tapTabBarItemView1 setTitle:@"管理" imageName:@"tabBar_2.png" tag:1]; 
    [tabBarView addSubview:tapTabBarItemView1]; 
    
    TapTabBarItemView *tapTabBarItemView2=[[[TapTabBarItemView alloc] init] autorelease]; 
    [tapTabBarItemView2 setTitle:@"记录" imageName:@"tabBar_3.png" tag:2]; 
    [tabBarView addSubview:tapTabBarItemView2]; 
    
    TapTabBarItemView *tapTabBarItemView3=[[[TapTabBarItemView alloc] init] autorelease]; 
    [tapTabBarItemView3 setTitle:@"更多" imageName:@"tabBar_4.png" tag:3]; 
    [tabBarView addSubview:tapTabBarItemView3]; 
    
    [self performSelector:@selector(initTabBarArray)]; 
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"change.tabbar.view" object:[NSNumber numberWithInt:0]]; 

-(void)initTabBarArray 

    //添加监听器 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTabBarView:) name:@"change.tabbar.view" object:nil]; 
    self.tabBarArray=[[NSMutableArray alloc] init]; 
    UIViewController1 *meetingRoomViewController =[[UIViewController1 alloc] initWithNibName:nil bundle:nil]; 
    UINavigationController *navController1 =[[[UINavigationController alloc] initWithRootViewController:meetingRoomViewController] autorelease]; 
    navController1.navigationBar.tintColor = [UIColor colorWithRed:75.0/255  green:118.0/255  blue:164.0/255  alpha:1]; 
    navController1.title=@"会议室"; 
    [meetingRoomViewController release]; 
    
    UIViewController2 *contactsBarViewController =[[UIViewController2 alloc] initWithNibName:nil bundle:nil]; 
    contactsBarViewController.title=@"通讯录"; 
    UINavigationController *navController2 = [[[UINavigationController alloc] initWithRootViewController:contactsBarViewController] autorelease]; 
    navController2.navigationBar.tintColor = [UIColor colorWithRed:75.0/255  green:118.0/255  blue:164.0/255  alpha:1]; 
    navController2.title=@"通讯录"; 
    [contactsBarViewController release]; 
    
    UIViewController3 *minutesViewController =[[UIViewController3 alloc] initWithNibName:nil bundle:nil]; 
    minutesViewController.title=@"会议记录";    
    UINavigationController *navController3 = [[[UINavigationController alloc] initWithRootViewController:minutesViewController] autorelease]; 
    navController3.title=@"会议记录"; 
    navController3.navigationBar.tintColor = [UIColor colorWithRed:75.0/255  green:118.0/255  blue:164.0/255  alpha:1]; 
    [minutesViewController release]; 
    
    UIViewController4 *moreControllerView =[[UIViewController4 alloc] initWithNibName:nil bundle:nil]; 
    moreControllerView.title=@"更多"; 
    UINavigationController *navController4 = [[[UINavigationController alloc] initWithRootViewController:moreControllerView] autorelease]; 
    [navController4.navigationBar setFrame:CGRectMake(0, 0, 320, 44)]; 
    navController4.navigationBar.tintColor = [UIColor colorWithRed:75.0/255  green:118.0/255  blue:164.0/255  alpha:1]; 
    [moreControllerView release]; 
    
    NSArray *array=[NSArray arrayWithObjects:navController1,navController2,navController3,navController4,nil]; 
    [tabBarArray addObjectsFromArray:array]; 

-(void)changeTabBarView:(NSNotification *)notifaction 

    int tabBarIndex=[[notifaction object] intValue]; 
    UINavigationController *navigationController=[self.tabBarArray objectAtIndex:tabBarIndex]; 
    for (UIView  *v in contentView.subviews) { 
        [v removeFromSuperview]; 
    } 
    UINavigationBar *barView=[navigationController navigationBar]; 
    [barView setFrame:CGRectMake(0, 0, 320, 44)]; 
    [navigationController popToRootViewControllerAnimated:YES]; 
    [contentView addSubview:navigationController.view]; 
}

主要是实现了一个NSArray数组,在这个里面放入每个选项的控制器,当点击下面一个tabbaritem时,就会发送一个通知,这时候从array中找到相应的控制器,进行显示即可。

下面是item显示的代码:

-(void)setTitle:(NSString *)title imageName:(NSString *)imageName tag:(int)tag 

    [self setTag:tag]; 
    [self setFrame:CGRectMake(80*tag,0, 80, 48)]; 
    [self setBackgroundColor:[UIColor clearColor]]; 
    UIImageView *imageView=[[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease]; 
    [imageView setContentMode:UIViewContentModeScaleAspectFit]; 
    [imageView setFrame:CGRectMake(21, 0, 38, 38)]; 
    [self addSubview:imageView]; 
    
    UILabel *titleLable=[[[UILabel alloc] initWithFrame:CGRectMake(0, 35, 80, 12)] autorelease]; 
    UIFont* uifont = [UIFont systemFontOfSize:10.0]; 
    [titleLable setFont:uifont]; 
    [titleLable setBackgroundColor:[UIColor clearColor] ]; 
    [titleLable setText:title]; 
    [titleLable setTextColor:[UIColor whiteColor]]; 
    [titleLable setTextAlignment:UITextAlignmentCenter]; 
    [self addSubview:titleLable]; 
    
    UITapGestureRecognizer *tapGesture=[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)] autorelease]; 
    [self addGestureRecognizer:tapGesture]; 
    

-(void)handleTapFrom:(UIGestureRecognizer *)gesture 

     [[NSNotificationCenter defaultCenter] postNotificationName:@"change.tabbar.view" object:[NSNumber numberWithInt:self.tag]]; 
}

程序的源代码:http://easymorse-iphone.googlecode.com/svn/trunk/TapController/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值