Question

I am trying to change images of my tabbar in a ViewController, but to display the new images, I must click on each tab bar item.

for (CustomTabBarItem *myItem in self.tabBarController.tabBar.items){
        myItem.enabled = YES; 

        myItem.badgeValue = @"1"; 

        UIImage *myImage =  [[UIImage alloc] initWithContentsOfFile:[[DesignManager sharedManager] getPathOfFile:@"test.png"]];

        *myItem.imageSelect= *myImage; // change images of each item. don't appear if I dont click on the item
}

Anyone know How can I can display directly these images? Thanks

Was it helpful?

Solution

You need to replace the old tab bar item with a new one. You can't update the image dynamically otherwise.

The easiest way to do this is to set the tabBarItem property of the view-controller represented by a given tab. If you wanted to do this from within that view controller, just write:

self.tabBarItem = [[UITabBarItem alloc] initWithTitle: @"title" image: myImage: tag: nil];

Or, you could do this from somewhere else, say your app delegate:

UIViewController* vc = [tabBarController.viewControllers objectAtIndex: 3];
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle: @"title" image: myImage: tag: nil];

OTHER TIPS

I know this is an old question. I ran into the same problem when I need to update the badge value from another active tab. Creating another UITabBarItem will solve your current problem but causes potential memory leak when this code is called many times. Plus, when other view controllers access the tab, they do not have reference to newly created UITabBarItem. My trick is

vc.tabBarItem = vc.tabBarItem; 

It works for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top