0

我正在开发一个 iOS 应用程序,我需要将标签栏自定义为如下所示:

在此处输入图像描述 标签栏图像

网络搜索给了我这个解决方案:

[self.tabBarItem setFinishedSelectedImage:<#(UIImage *)#> withFinishedUnselectedImage:<#(UIImage *)#>]

但它适用于iOS5。是否有适用于 iOS4 和 iOS5 的解决方案?

4

2 回答 2

0

我们使用下面的代码。您可以自定义标签栏

self.tabBarController = [[UITabBarController alloc]init];
firstClassobj.tabBarItem = [[UITabBarItem alloc]
                     initWithTitle:NSLocalizedString(@"yourtext", @"yourtext)
                     image:[UIImage imageNamed:@"your Image"]
                     tag:0];
secondClassobj.tabBarItem = [[UITabBarItem alloc]
                     initWithTitle:NSLocalizedString(@"yourtext", @"yourtext)
                     image:[UIImage imageNamed:@"your Image"]
                     tag:0];
self.tabBarController .viewControllers = [NSArray arrayWithObjects:firstClassobj,secondClassobj,nil];
于 2012-11-06T07:02:53.720 回答
0

//这是我成功实现的代码......

//在.h中设置委托UITabBarControllerDelegate,UITabBarDelegate

    UINavigationController *navigationController;
    UITabBarController *tabBarController;
    @property(strong,nonatomic) UINavigationController *navigationController;
    @property(strong,nonatomic) UITabBarController *tabBarController;

//在.m

  tabBarController = [[UITabBarController alloc] init];
  [tabBarController setDelegate: self];

//用于标签栏背景 tabBarController.tabBar.tintColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbg.png"]];

NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];

Demo1 *home = [[Login alloc] initWithNibName:@"Login" bundle:nil];
    localNavController = [[UINavigationController alloc] initWithRootViewController:home];
localNavController.tabBarItem.title=@"demo1";
[localViewControllersArray addObject:localNavController];
    Demo2 *puck=[[Demo2 alloc]initWithNibName:@"Demo2" bundle:nil];
    localNavController = [[UINavigationController alloc] initWithRootViewController:puck];
localNavController.tabBarItem.title=@"demo2";
[localViewControllersArray addObject:localNavController];
    Demo3 *photo=[[Demo3 alloc]initWithNibName:@"Demo3" bundle:nil];
    localNavController = [[UINavigationController alloc] initWithRootViewController:photo];
localNavController.tabBarItem.title=@"demo3";
    [localViewControllersArray addObject:localNavController];
    Demo4 *more=[[Demo4 alloc]initWithNibName:@"Demo4" bundle:nil];
    localNavController = [[UINavigationController alloc] initWithRootViewController:more];
localNavController.tabBarItem.title=@"demo4";
[localViewControllersArray addObject:localNavController];

  [[UITabBarItem appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor grayColor], UITextAttributeTextColor, 
  [UIColor grayColor], UITextAttributeTextShadowColor, 
  [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset, 
  [UIFont fontWithName:@"Helvetica" size:0.0], UITextAttributeFont, 
  nil] 
                                         forState:UIControlStateNormal];

//标签栏选中和未选中的图标

UITabBarItem *tabBarItem1 = [[self.tabBarController.tabBar items] objectAtIndex:0];
[tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"puckCentralA.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"puckCentral.png"]];
UITabBarItem *tabBarItem2 = [[self.tabBarController.tabBar items] objectAtIndex:1];
[tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"puckDisplayA.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"puckDisplay.png"]];
UITabBarItem *tabBarItem3 = [[self.tabBarController.tabBar items] objectAtIndex:2];
[tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"photoBoothA.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"photoBooth.png"]];
UITabBarItem *tabBarItem4 = [[self.tabBarController.tabBar items] objectAtIndex:3];
[tabBarItem4 setFinishedSelectedImage:[UIImage imageNamed:@"moreA.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"more.png"]];

[self.window addSubview:tabBarController.view];

于 2013-05-02T13:10:47.490 回答