Search code examples
iosuitabbar

Change Image of UItabbar Item, Using storyboards


I have the following story board:

enter image description here

As you can see there is a tab bar application with 5 tabs, on the storyboard I've assign the logo for each tab. Now when the user clicks a cell in a particular view I want to change the image of one of the tabs. How can I do this? I don't have an instance of the tab bar view controller or items since storyboards pretty much does all this for me. So my question is what methods do I have to implement to change the image? If I need the tab bar controller how can I get its instance and in which class should I point it to?

Thank you very much,


Solution

  • In any UIViewController class that is part of the Tab Bar hierarchy, all you have to do to get in instance of the tab bar controller is:

    //In UIViewController
    UITabBarController *tabBarController = self.tabBarController;
    

    You can then change the image as so

    //Suppose you want to change the 1st (0th) tab bar image
    UITabBarItem * tabItem = [tabBarController.tabBar.items objectAtIndex: 0];
    tabItem.image = //whatever image you want to change to