如何创建自定义标签栏...在标签栏中添加自定义图像(无需 xib 更改)

Posted

技术标签:

【中文标题】如何创建自定义标签栏...在标签栏中添加自定义图像(无需 xib 更改)【英文标题】:how can i create custom tab bar ...add custom images in tab bar (without xib changes) 【发布时间】:2011-01-17 05:37:17 【问题描述】:

我在该标签栏中有一个应用程序是自定义的,并且所有图像都是不同的颜色...

如何创建具有不同颜色和不同图像的标签栏..此标签栏具有导航视图。

【问题讨论】:

【参考方案1】:

我得到了答案......

h文件代码...

@interface CustomTabBarController : UITabBarController 



@end


@interface AroundParisAppDelegate : NSObject <UIApplicationDelegate,UITabBarControllerDelegate> 
 UIImageView *imgTab ,*imghome ,*imgQuiz,*imgtTW, *imgGuest;
IBOutlet CustomTabBarController *tabBarController;

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic,retain) IBOutlet CustomTabBarController *tabBarController;
-(void)tabBarControllerView;
@end

在m文件中:---

@synthesize tabBarController;


-(void)tabBarControllerView

    tabBarController = [[UITabBarController alloc] init];
    tabBarController.delegate = self;

    Home *firstTabController =[[Home alloc]initWithNibName:@"Home" bundle:nil];

    //-----second Tab   -----//

    quiz *secondTabController = [[quiz alloc]initWithNibName:@"quiz" bundle:nil];


    //------3rd tab--//
    tw *thirdControllerMap = [[tw alloc] initWithNibName:@"tw" bundle:nil];

    //-----4th tab bar--------//
    guest *fourthTabController = [[guest alloc] initWithNibName:@"guest" bundle:nil];


    tabBarController.viewControllers = [NSArray arrayWithObjects:firstTabController, secondTabController,thirdControllerMap,fourthTabController, nil];

    UINavigationController *objnavifirst=[[UINavigationController alloc] initWithRootViewController:firstTabController];
    UINavigationController *objnavisecond=[[UINavigationController alloc] initWithRootViewController:secondTabController];
    UINavigationController *objnavithird=[[UINavigationController alloc] initWithRootViewController:thirdControllerMap];
    UINavigationController *objnavifourth=[[UINavigationController alloc] initWithRootViewController:fourthTabController];

    NSMutableArray *MainNavArray =[[NSMutableArray alloc] init];
    objnavifirst.navigationBarHidden=YES;
    objnavisecond.navigationBarHidden=YES;
    objnavithird.navigationBarHidden=YES;
    objnavifourth.navigationBarHidden=YES;

    [MainNavArray  addObject:objnavifirst];
    [MainNavArray  addObject:objnavisecond];
    [MainNavArray  addObject:objnavithird];
    [MainNavArray  addObject:objnavifourth];

    tabBarController.viewControllers =MainNavArray; 
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible]; 

    imgTab = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320.0,50)];
    imgTab.image=[UIImage imageNamed:@"Bottom-bar.png"];
    tabBarController.tabBar.tag=10;
    [tabBarController.tabBar addSubview:imgTab];

    imghome=[[UIImageView alloc]initWithFrame:CGRectMake(2.0,00.0,76.0,49.0)];
    imghome.image=[UIImage imageNamed:@"home-hover.png"];
    [imgTab addSubview:imghome];

    imgQuiz=[[UIImageView alloc]initWithFrame:CGRectMake(82.0,00.0,76.0,49.0)];
    imgQuiz.image=[UIImage imageNamed:@"quiz.png"];
    [imgTab addSubview:imgQuiz];

    imgtTW=[[UIImageView alloc]initWithFrame:CGRectMake(164.0,00.0,76.0,49.0)];
    imgtTW.image=[UIImage imageNamed:@"twitter.png"];
    [imgTab addSubview:imgtTW];

    imgGuest=[[UIImageView alloc]initWithFrame:CGRectMake(244.0,00.0,76.0,49.0)];
    imgGuest.image=[UIImage imageNamed:@"guest.png"];
    [imgTab addSubview:imgGuest];

    tabBarController.view.hidden = NO;
    tabBarController.selectedIndex = 1;



- (void)tabBarController:(UITabBarController *)tabBarControllers didSelectViewController:(UIViewController *)viewController  


    NSLog(@"%i",tabBarControllers.selectedIndex);
    if (tabBarControllers.selectedIndex == 0)
    

        imghome.image=[UIImage imageNamed:@"home-hover.png"];
        imgQuiz.image=[UIImage imageNamed:@"quiz.png"];
        imgtTW.image=[UIImage imageNamed:@"twitter.png"];
        imgGuest.image=[UIImage imageNamed:@"guest.png"];

    
    else if (tabBarControllers.selectedIndex == 1)
    

        imghome.image=[UIImage imageNamed:@"home.png"];
        imgQuiz.image=[UIImage imageNamed:@"quiz-hover.png"];
        imgtTW.image=[UIImage imageNamed:@"twitter.png"];
        imgGuest.image=[UIImage imageNamed:@"guest.png"];

    
    else if (tabBarControllers.selectedIndex == 2)
    

        imghome.image=[UIImage imageNamed:@"home.png"];
        imgQuiz.image=[UIImage imageNamed:@"quiz.png"];
        imgtTW.image=[UIImage imageNamed:@"twitter-hover.png"];
        imgGuest.image=[UIImage imageNamed:@"guest.png"];

    
    else if (tabBarControllers.selectedIndex == 3)
    

        imghome.image=[UIImage imageNamed:@"home.png"];
        imgQuiz.image=[UIImage imageNamed:@"quiz.png"];
        imgtTW.image=[UIImage imageNamed:@"twitter.png"];
        imgGuest.image=[UIImage imageNamed:@"guest-hover.png"];

    




这是添加标签栏的完整示例

【讨论】:

非常感谢您提供本教程。我在我的应用程序中实现了,工作正常。

以上是关于如何创建自定义标签栏...在标签栏中添加自定义图像(无需 xib 更改)的主要内容,如果未能解决你的问题,请参考以下文章

Phonegap:在标签栏中添加自定义图标

Ios Swift:在自定义标签栏中显示导航栏

自定义标签栏问题

如何让自定义标签栏显示标签栏项目在 Xcode 中设置的选定图像?

如何在导航栏中添加自定义视图?

IOS Segue 在自定义标签栏中不起作用