Question

I have an issue with the images of tabbar items. I set an image on each tabbar item but when i run the app, the only image that appears is that of the first tabbar item. Images of others tabbar items don't appear until i select one of these tabs. The code that i use to set an image on a tabbar item is the following:

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

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

Can someone help me?

Was it helpful?

Solution

You can try this:

[[[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"]];

OTHER TIPS

[[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"]];

use this code :--

- (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];

Creates and returns a new item with the specified title, unselected image, and selected image. If no selectedImage is provided, image is used as both the unselected and selected image. By default, the actual unselected and selected images are automatically created from the alpha values in the source images. To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal. Availability iOS 7 and later.

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];

I would prefer that the ViewController do this. So, in my opinion, the best solution should be override init method and set the TabBarItem here.

Probably you are doing it in viewDidLoad and so only the first item appear.

Sorry for the spelling mistakes.

SWIFT 5.4

  tabBarController?.viewControllers?[index].tabBarItem.image = UIImage(named: "imageName")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top