在UITabBarItem上使用自定义选定图像和未选定图像

时间:2013-03-20 10:18:54

标签: ios objective-c uitabbarcontroller uitabbar

我正在尝试将自己的图像设置为UITabBarItem上的选定图像和未选择图像,但没有运气。我在视图控制器viewDidLoadviewWillAppear方法中尝试了以下代码,但都不起作用:

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"on_rojo"] withFinishedUnselectedImage:[UIImage imageNamed:@"on"]];

我做错了什么?我也读过:iOS 5: UITabBarItem setFinishedSelectedImage:withFinishedUnselectedImage: not working / ignored,但我在IB上创建了我的UITabBarController,而不是以编程方式创建。这是问题吗?

提前谢谢你,

2 个答案:

答案 0 :(得分:13)

您必须为每个标签项指定图像。以下代码适用于我

if ([[tabBarController.tabBar.items objectAtIndex:0] respondsToSelector:@selector(setFinishedSelectedImage:withFinishedUnselectedImage:)]) {

  [[tabBarController.tabBar.items objectAtIndex:0] setFinishedSelectedImage:[UIImage imageNamed:@"blabla1.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"bleble1.png"]];
  [[tabBarController.tabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:@"blabla2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"bleble2.png"]];
  [[tabBarController.tabBar.items objectAtIndex:2] setFinishedSelectedImage:[UIImage imageNamed:@"blabla3.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"bleble3.png"]];
  [[tabBarController.tabBar.items objectAtIndex:3] setFinishedSelectedImage:[UIImage imageNamed:@"blabla4.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"bleble4.png"]];

}

答案 1 :(得分:0)

尝试这个,我用过这个:

ALTabbar