问题
I'm developing a shopping cart tab. Originally I just use the default badge value to show how many items in the cart on the bottom tabbar. Now the designer wants to be fancy, he wants to show different image based on how many items in the cart. For example, if there's one, show cartTab-1.png, if 2, show cartTab-2.png...
I tried to change the tabaritem (UITabBarItem
)'s image but it didn't work for me. Is it feasible? I discussed with my colleague, he said I may have to draw the image on top of the tabbarItem by myself. Do you have any suggestion? Thanks
more details:
- I created the tabItem using InterfaceBuilder, and set the image and title over there
- I need to support ios4. So I can't use the setSelectedImage...
- In my case it's a KVO, if the cart count changes, it notifies the method to update the image. not in the initialize step.
does anyone know why [self.tabBarItem setImage:[UIImage imageNamed:@"cartxxx.png"]]
doesn't work? When I debug, the property do changed, but the UI remains same
Update
the below code works. Thanks everyone!
UIImage* cartTabImage = [UIImage imageNamed:cartTabImageName];
[[self.tabBarController.tabBar.items objectAtIndex:3] setImage:cartTabImage];
回答1:
Swift 3.0 version for 2 tabs,
self.tabBar.items?[0].image = UIImage(named: "inactive_image_0")?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[0].selectedImage = UIImage(named: "active_image_0")?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[1].image = UIImage(named: "inactive_image_1")?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[1].selectedImage = UIImage(named: "active_image_1")?.withRenderingMode(.alwaysOriginal)
回答2:
UITabBarItem *tabBarItem0 = [self.tabBarController.tabBar.items objectAtIndex:0];
[tabBarItem0 setImage:[[UIImage imageNamed:@"iconGray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem0 setSelectedImage:[[UIImage imageNamed:@"iconBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
回答3:
The simplest way I found was
self.tabBarItem.image=[UIImage imageNamed:@"myImage.png"];
回答4:
Try this:
int numItems = 0; // count of items in your shopping cart
NSString *imageName = [NSString stringWithFormat:@"cartTab-%d",numItems];
// change your image
[[self.tabBar.items objectAtIndex:myIndex] setImage:[UIImage imageNamed:imageName]];
// or, if you want to set it when initializing the tabBar
UITabBarItem *item = [[[UITabBarItem alloc] initWithTitle:myTitle image:[UIImage imageNamed:imageName] tag:someTag];
回答5:
This Answer May be Help You
UITabBarItem *i5=[[UITabBarItem alloc]initWithTitle:@"Profile" image:[UIImage imageNamed:@"profile.png"] tag:5];
回答6:
- (void)setFinishedSelectedImage:(UIImage *)selectedImage withFinishedUnselectedImage:(UIImage *)unselectedImage
selectedImage is displayed when the user has selected the tab. unselectedImage is displayed when the user has selected a different tab.
in your viewDidLoad:
do
UIImage *c1 = [UIImage imageNamed:@"cart1.png"];
UIImage *c2 = [UIImage imageNamed:@"cart1unselected.png"];
[[self tabBarItem] setFinishedSelectedImage:c1 withFinishedUnselectedImage:c2];
回答7:
As mentioned on the updated question and other answers, in most cases the UITabBarItem
needs to be accessed via the UITabBarController
directly.
It seems that iOS creates a copy of the UITabBarItem
instance, which explains why updating the self.tabBarItem
property does not reflect the changes in the user interface:
My guess is that this happens when the Tab Bar items are created programmatically, instead of by the storyboard, but this is just a guess.
The solution is then, as pointed out, accessing the array of Tab Bar items via the Tab Bar controller. This solution is bad in that it depends on the knowledge of the tab bar item index:
UITabBarItem *tabBarItem = [self.tabBarController.tabBar.items objectAtIndex:0];
[tabBarItem setImage:image];
[tabBarItem setSelectedImage:image];
Don't forget to update both images for default and selected states.
回答8:
let favorites = UITabBarItem(title: nil, image:UIImage(named: "Screen Shot 2018-12-13 at 11.00.42 AM") , tag: 0)
来源:https://stackoverflow.com/questions/8847483/how-to-programmatically-change-the-tabbaritems-image