2

In my app, one of the TabBar Items needs a custom icon, depending on context within the app. I'm using this code to accomplish that:

item1 = [[UITabBarItem alloc] initWithTitle:@"Jij" image:inactiveIcon selectedImage:activeIcon];

and that works just fine. As long as I don't set an icon in the Storyboard.

In my experiments, I found out that running above code from the AppDelegate's didFinishLaunchingWithOptions executes the code before the TabBar even exists. At least that is what I think.

So, in order to have at least a temporary placeholder icon, I have to set it in the Storyboard. But somehow, that keeps my code from updating the image.

I even looked into subclassing the TabBarController, just to make sure that the icon is loaded in time.

Please point me at the mistakes in my code, logic or findings.

To summarize the question:

How come I cannot change an TabBarItem image once it is set in the Storyboard?

Sjakelien
  • 2,255
  • 3
  • 25
  • 43

1 Answers1

4

You can also set the property Render As of your tab bar images within your asset catalog directly. enter image description here

There you have the option to set the property to Default, Original Image and Template Image. Hope this will help :)

N.Raval
  • 549
  • 3
  • 11
  • I tried all three options, but still the image from the asset catalog prevails, i.e.: it remains in place after I run my method. However, if I don't set that image in the Storyboard, it shows up empty at first, but after running my method, the custom image IS shown. – Sjakelien May 06 '16 at 09:23
  • @Sjakelien Select the UITabbarItem and add a run-time attribute 'selectedImage', select Type as 'Image' and give the name of your image as its value. may be this will help you. – N.Raval May 06 '16 at 09:27
  • I must be doing something wrong. I tried your suggestion, both WITH and WITHOUT a placeholder image, but the result is the same. – Sjakelien May 06 '16 at 10:10
  • it worked for me...changing "Render As key from Default image to Original image"....worked. – Nilesh Kumar Sep 08 '16 at 09:31