1

我有一个带有 3 个项目的 UITabbar 控制器,我想要颜色图标而不是基于 Gary 图标,

请你给我一些提示,我怎样才能在 tababr 中有颜色图标,

这是我的代码:

self.title = @"test";

   self.tabBarItem = [[UITabBarItem alloc] initWithTitle:self.title image:[UIImage 
 imageNamed:@"test"] tag:0];

通常 test 是一个带有彩色图片的图标,但在 UITabbar 中它只是 Gary,

提前致谢!

4

3 回答 3

4

使用此代码:

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"test"] 
withFinishedUnselectedImage:[UIImage imageNamed:@"test"]];
于 2013-03-26T19:27:39.270 回答
0

您将需要构建自己的 TabBarController。根据 Apple 文档关于“此类 [UITabBarController] 不适用于子类化”的问题。UITabBarItem 上的文档说,当您为标签栏提供图像时,“标签栏上显示的图像来自此图像”。因此,您提供给标签栏的任何图像都会被处理以使其符合标签栏图像的“正常”外观。

因此,您可以使用一些 UIButtons 作为子视图构建一个 UIViewController,然后以这种方式管理整个外观。

于 2013-03-25T04:02:47.517 回答
0

您必须为所有 3 个选项卡创建 2 个图像,其中一个未选中,第二个已选中。

之后在您的 appdelegate.m 文件中写入以下代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
       self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
       // Override point for customization after application launch.
       self.searchArray = [[NSMutableArray alloc]init];
       self.tabBarController = [[[UITabBarController alloc] init] autorelease];

       viewController1 *view1 = [[[viewController1 alloc] initWithNibName:@"viewController1" bundle:nil] autorelease];
       view1.tabBarItem.image = [UIImage imageNamed:@"tab-selected-1"];
       view1.tabBarItem.title = @"Title1";           

       viewController2 *view2 = [[[viewController2 alloc] initWithNibName:@"viewController2" bundle:nil] autorelease];
       view2.tabBarItem.image = [UIImage imageNamed:@"tab-selected-2"];
       view2.tabBarItem.title = @"Title2";

       viewController3 *view3 = [[[viewController3 alloc] initWithNibName:@"viewController3" bundle:nil] autorelease];
       view3.tabBarItem.image = [UIImage imageNamed:@"tab-selected-3"];
       view3.tabBarItem.title = @"Title3";



       self.tabBarController.viewControllers = [NSArray arrayWithObjects:view1, view2, view3, nil];

       UITabBarItem *tabBarItem1 = [[self.tabBarController.tabBar items] objectAtIndex:0];
       [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"tab-selected-1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab-unselected-1.png"]];


       UITabBarItem *tabBarItem2 = [[self.tabBarController.tabBar items] objectAtIndex:1];
       [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"tab-selected-2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab-unselected-2.png"]];

       UITabBarItem *tabBarItem3 = [[self.tabBarController.tabBar items] objectAtIndex:2];
       [tabBarItem3 setFinishedSelectedImage:[UIImage imageNamed:@"tab-selected-3.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tab-unselected-3.png"]];


       return YES;
}

试试这个,你的问题就解决了。祝你好运

于 2013-03-25T04:07:09.130 回答