4

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

Hi Tom, do you have this for Swift 5.1, iOS 12? I have this question :stackoverflow.com/questions/58942737/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.