5

我对标签栏项目的图像有疑问。我在每个标签栏项目上设置了一个图像,但是当我运行应用程序时,唯一出现的图像是第一个标签栏项目的图像。在我选择这些选项卡之一之前,不会出现其他选项卡栏项目的图像。我用来在标签栏项目上设置图像的代码如下:

[self.tabBarItem setImage:[UIImage imageNamed:@"multi30x30.png"]];

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"multi30x30.png"]
              withFinishedUnselectedImage:[UIImage imageNamed:@"multi30x30.png"]];

有人能帮我吗?

4

6 回答 6

7

你可以试试这个:

[[[self.tabBarController.viewControllers objectAtIndex:0] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"LocateIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"LocateIconInactive.png"]];

[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"ProductsIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"ProductsIconInactive.png"]];

[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setFinishedSelectedImage:[UIImage imageNamed:@"NextDeliveryIconActive.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"NextDeliveryIconInactive.png"]];
于 2013-11-08T09:45:49.193 回答
1
[[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"]];
于 2013-11-08T09:10:55.857 回答
1

使用此代码:-

- (BOOL)application:(UIApplication *)application
 didFinishLaunchingWithOptions:(NSDictionary 
  *)launchOptions
 {


UITabBarController *tabBarController 
= (UITabBarController 
 *)self.window.rootViewController;

UIImage *selectedImage0 = [UIImage
   imageNamed:@"tb_inorganic_selected"];
UIImage *unselectedImage0 = [UIImage imageNamed:@"tb_inorganic_normal"];

UIImage *selectedImage1 = [UIImage imageNamed:@"tb_organic_selected"];
UIImage *unselectedImage1 = [UIImage imageNamed:@"tb_organic_normal"];

UIImage *selectedImage2 = [UIImage imageNamed:@"tb_acid_selected"];
UIImage *unselectedImage2 = [UIImage imageNamed:@"tb_acid_normal"];



UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];

[item0 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];
[item1 setFinishedSelectedImage:selectedImage1 withFinishedUnselectedImage:unselectedImage1];
[item2 setFinishedSelectedImage:selectedImage2 withFinishedUnselectedImage:unselectedImage2];
于 2013-11-08T09:13:12.977 回答
0

创建并返回具有指定标题、未选定图像和选定图像的新项目。如果未提供 selectedImage,则 image 将用作未选择和已选择的图像。默认情况下,实际未选择和选择的图像是根据源图像中的 alpha 值自动创建的。为防止系统着色,请使用 UIImageRenderingModeAlwaysOriginal 提供图像。可用性iOS 7及更高版本。

UIImage *aImage = [UIImage imageNamed:@"a"]; 
aImage = [aImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIImage *inaImage = [UIImage imageNamed:@"ina"];
inaImage = [inaImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

self.aController.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"title" image:inaImage selectedImage:aImage];
于 2013-11-08T09:44:24.697 回答
0

我希望 ViewController 这样做。因此,在我看来,最好的解决方案应该是覆盖 init 方法并在此处设置 TabBarItem。

可能您正在 viewDidLoad 中执行此操作,因此仅出现第一项。

很抱歉拼写错误。

于 2015-07-01T17:29:57.243 回答
0

SWIFT 5.4

  tabBarController?.viewControllers?[index].tabBarItem.image = UIImage(named: "imageName")
于 2021-06-23T18:04:45.000 回答