Reputation: 1965
At some point during runtime, I want to change the image for one of the tabs in my UITabBar
. Here is what I have tried so far:
[[self.tabBarController.tabBar.items objectAtIndex:1]
setImage:[UIImage imageNamed:@"image-name"]
forState:UIControlStateNormal];
The above gives me a -[UITabBarItem setImage:forState:]: unrecognized selector sent to instance
If I use the setImage
method without forState
it works, but this method was deprecated in iOS 3.
I tried your answers, but now there's this weird blue line above the UITabBar's
UIIMage
I changed. Any idea why?
Upvotes: 4
Views: 5649
Reputation: 1760
though the question is very old and the answer by @visput absolutely working but for some one complete newbie like me
In your UITabBarController
implementation
if while loading of the UITabBarController
you want, put it in viewDidLoad
method
UITabBarItem *item = [self.tabBar.items objectAtIndex:ITEM_INDEX];// item index is the tab index which starts from 0
item.image = [UIImage imageNamed:@"image"];
item.selectedImage = [UIImage imageNamed:@"image"];
If you want to change it runtime like on selection of something, put this code in viewDidLayoutSubviews
method
Upvotes: 1
Reputation: 318804
UITabBarItem
extends UIBarItem
which has an image
property.
Do:
[[self.tabBarController.tabBar.items objectAtIndex:1] setImage:[UIImage imageNamed:@"image-name"]];
Though it would be easier to read and debug as follows:
UITabBarItem *item = self.tabBarController.tabBar.items[1];
item.image = [UIImage imageNamed:@"image-name"];
Upvotes: 1
Reputation: 13302
Use image
and selectedImage
properties:
UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:1];
item.image = [UIImage imageNamed:@"image"];
item.selectedImage = [UIImage imageNamed:@"selected_image"];
Also pay attention on this:
By default, the actual selected image is automatically created from the alpha values in the source image. To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal.
Upvotes: 6