码迷,mamicode.com
首页 > 其他好文 > 详细

自定义UITabBarController

时间:2014-05-08 10:41:38      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:blog   class   code   tar   ext   color   

网上大多的自定义TabBar都是继承View的,项目中要用到path+Tabbat这样的话(path用的MMDrawerController这个框架),继承View的Tabbar是无法满足条件的(不是一个容器)。下面就介绍下自己写的一个自定义TabBar。

和系统自带的写法差不多,先看下原生的写法如下:

首先在.h中自定义一个UITabBarController* tabBarViewController; 接着在.m中来做实现,

tabBarViewController = [[UITabBarController alloc]init];
   
    
    FirstViewController* first = [[FirstViewController alloc]init];
    UINavigationController * navOne = [[UINavigationController alloc] initWithRootViewController:first ];
    [navOne setNavigationBarHidden:TRUE];
   
    SecondViewController* second = [[SecondViewController alloc]init];
    UINavigationController * navtwo = [[UINavigationController alloc] initWithRootViewController:second ];
    [navtwo setNavigationBarHidden:TRUE];
    
    ViewController3 *vc3 = [[ViewController3 alloc] init];
    UINavigationController * nav3 = [[UINavigationController alloc] initWithRootViewController:vc3 ];
    [nav3 setNavigationBarHidden:TRUE];
    ViewController5 *vc5 = [[ViewController5 alloc] init];
    UINavigationController * nav5 = [[UINavigationController alloc] initWithRootViewController:vc5 ];
    [nav5 setNavigationBarHidden:TRUE];
    
    tabBarViewController.viewControllers = [NSArray arrayWithObjects:navOne, navtwo,nav3,nav5, nil];
    [first release];
    [second release];
    [vc3 release];
    [vc5 release];
    
    UITabBar *tabBar = tabBarViewController.tabBar;
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
    UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
    UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
    
    tabBarItem1.title = @"消息";
    tabBarItem2.title = @"应用";
    tabBarItem3.title = @"通讯录";
    tabBarItem4.title = @"我";
    
    [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"home_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home.png"]];
    [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"maps_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"maps.png"]];
    [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"myplan_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"myplan.png"]];
    [tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"settings_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"settings.png"]];
    UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
   [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor whiteColor], UITextAttributeTextColor,
                                                       nil] forState:UIControlStateNormal];
   UIColor *titleHighlightedColor = [UIColor greenColor];
   [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       titleHighlightedColor, UITextAttributeTextColor,
                                                       nil] forState:UIControlStateSelected];


下面来看下自定义的方法,也是在.h文件中定义

UITabBarController* tabBarViewController; 

在定义一个long preSelect变量,这个变量用来判断点击是哪个selectedIndex。

在.m中的具体实现如下:

 NSMutableArray *arrController = [[NSMutableArray alloc]init];
    //信息
    MessageViewController *messageVC = [[MessageViewController alloc]initWithNibName:@"MessageViewController" bundle:nil];
    
    UINavigationController * navMessage = [[UINavigationController alloc] initWithRootViewController:messageVC ];
    [messageVC release];
    
    [navMessage setNavigationBarHidden:TRUE];
    [arrController addObject:navMessage];
    [navMessage release];
    //应用
    AppsViewController *appsVC = [[AppsViewController alloc]initWithNibName:@"AppsViewController" bundle:nil];
    
    UINavigationController *navApps = [[UINavigationController alloc] initWithRootViewController:appsVC];
    [appsVC release];
    [navApps setNavigationBarHidden:TRUE];
    [arrController addObject:navApps];
    [navApps release];
    //通讯录
    AddressViewController *addressVC = [[AddressViewController alloc]initWithNibName:@"AddressViewController" bundle:nil];
    
    UINavigationController *navAddress = [[UINavigationController alloc] initWithRootViewController:addressVC];
    [addressVC release];
    [navAddress setNavigationBarHidden:TRUE];
    [arrController addObject:navAddress];
    [navAddress release];
    //我
    MeViewController *meVC = [[MeViewController alloc]initWithNibName:@"MeViewController" bundle:nil];
    
    UINavigationController *navMe = [[UINavigationController alloc] initWithRootViewController:meVC];
    [meVC release];
    [navMe setNavigationBarHidden:TRUE];
    [arrController addObject:navMe];
    [navMe release];
    
    
    tabBarViewController=[[UITabBarController alloc] init];
    
    tabBarViewController.viewControllers=arrController;
    [arrController release];
    preSelect = 1;
    for (int i = 0; i<4; i++) {
        NSString *nalString = [NSString stringWithFormat:@"tabbar_unselecte%d.png", i+1];
        NSString *highString = [NSString stringWithFormat:@"tabbar_selected%d.png", i+1];
        UIImage *nalImage = [UIImage imageNamed:nalString];
        UIImage *highImage = [UIImage imageNamed:highString];
        
        UIButton *tabbarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tabbarBtn.frame = CGRectMake(i*(320/4), 0, (320/4), 46);
        [tabbarBtn addTarget:self action:@selector(tabbarBtnPress:) forControlEvents:UIControlEventTouchUpInside];
        [tabbarBtn setBackgroundImage:nalImage forState:UIControlStateNormal];
        [tabbarBtn setBackgroundImage:highImage forState:UIControlStateSelected];
        tabbarBtn.tag = 100+i;
        [tabBarViewController.tabBar addSubview:tabbarBtn];
        
        if (i == 1) {
            tabbarBtn.selected = YES;
            tabBarViewController.selectedIndex=1;
        }
    }

主要是思路就是自定义UITabBarController的tabBar属性,在它上面addSubview。由于加的UIButton,现在就要实现点击

方法来切换。方法如下:

-(void)tabbarBtnPress:(UIButton *)tabbarBtn
{
    if (preSelect != tabbarBtn.tag -100 ) {
        
        UIButton *prvBtn = (UIButton *)[tabbarBtn.superview viewWithTag:preSelect+100];
        prvBtn.selected = NO;
        
        tabBarViewController.selectedIndex = preSelect = tabbarBtn.tag-100;
        
        UIButton *button = (UIButton *)[tabbarBtn.superview viewWithTag:tabbarBtn.tag];
        button.selected = YES;
    }
}

到此就完成了。有什么不对地方请大家指出来。




自定义UITabBarController,布布扣,bubuko.com

自定义UITabBarController

标签:blog   class   code   tar   ext   color   

原文地址:http://blog.csdn.net/quanqinyang/article/details/25273745

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!