iOS: Why is custom tab bar item showing only as grey silhouette? - ios

I am trying to set the tab bar icons of my UITabBarController with custom *.png files (one for the selected and one for unselected).
The images are all in non-interlaced png format and are named correctly (#2x, #3x, etc) residing in an *.imageset.
But the tab bar items are only shown as silhouettes like this:
I tried to set these images within Interface Builder, without success. Then I also tried to set them programmatically in the "loadView" function of MyTabBarController (which is extending UITabBarController) like this:
UIImage *selectedImage;
UIImage *unselectedImage;
// tab1
selectedImage = [UIImage imageNamed:#"cmdGamesActive"];
unselectedImage = [UIImage imageNamed:#"cmdGamesInactive"];
UITabBarItem *item1 = [self.tabBar.items objectAtIndex:0];
item1 = [item1 initWithTitle:#"Games" image:unselectedImage selectedImage:selectedImage];
// tab2
selectedImage = [UIImage imageNamed:#"cmdFriendsActive"];
unselectedImage = [UIImage imageNamed:#"cmdFriendsInactive"];
UITabBarItem *item2 = [self.tabBar.items objectAtIndex:1];
item2 = [item2 initWithTitle:#"Friends" image:unselectedImage selectedImage:selectedImage];
// tab3
selectedImage = [UIImage imageNamed:#"cmdTrophiesActive"];
unselectedImage = [UIImage imageNamed:#"cmdTrophiesInactive"];
UITabBarItem *item3 = [self.tabBar.items objectAtIndex:2];
item3 = [item3 initWithTitle:#"Trophies" image:unselectedImage selectedImage:selectedImage];
// tab4
selectedImage = [UIImage imageNamed:#"cmdSettingsActive"];
unselectedImage = [UIImage imageNamed:#"cmdSettingsInactive"];
UITabBarItem *item4 = [self.tabBar.items objectAtIndex:3];
item4 = [item4 initWithTitle:#"Settings" image:unselectedImage selectedImage:selectedImage];
... with the same result.
Any ideas how to solve this issue ?

According to this answer, I did something extra and kind of have answer for you here. I have my custom UITabBarController, which is linked with my UITabBarController in the StoryBoard file. So in order to remove the automatic tint provided by iOS when the TabBar is unselected, I ended up removing it in this manner. The images can be a vast variety of images but just in the way recommended here. Here it goes:
NSArray *navConArr = self.viewControllers;//self is custom UITabBarController
UINavigationController *naviOne = [navConArr objectAtIndex:0];//I have 3 different tabs, objectAtIndex:0 means the first tab navigation controller
UITabBarItem *naviBtn = naviOne.tabBarItem;
UIImage *image = [[UIImage imageNamed:#"iconNaviOne"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[naviBtn setSelectedImage:image];
[naviBtn setImage:image];
Thankfully, this works like a charm (:

I was facing a similar issue.
I had chosen few icons for my tabs such as 'mic', 'smiley' and 'settings'.
But the only things visible when I executed the program were grey and blue squares.
I modified my '.png' images to be transparent and now they are visible as they were intended to.
i.e, make sure the images used for tab icons do not have a background.

This should do the trick:
UITabBarItem *item0 = [_tabBar.items objectAtIndex:0];
item0.image = [[UIImage imageNamed:#"edit_profile_tab"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item0.selectedImage = [[UIImage imageNamed:#"edit_profile_tab_active"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

Related

UITabBarController has a truncated badge

I get the behavior from the picture below. Basically the badge from the second tab it's under the third UITabBarItem image. The image from third tab it's composed from the camera icon and the green background.
I am assigning an image using:
UITabBarItem *tabItem3 = [self.tabBar.items objectAtIndex:3];
tabItem3.selectedImage = [[UIImage imageNamed:#"albumsSelected"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabItem3.image = [[UIImage imageNamed:#"albums"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
Is there a way to fix this UI bug?

How to change center tab bar button colour in iOS 7?

I want the middle button in my UITabBarController to have a different colour to the others, like Instagram for example. Does anyone have any suggestions?
Instead of colour you should set every tabBarItem image differently for each one (If you want it just like instagram)
Here is an example for one:
UITabBarItem *tabBarItem = [yourTabBarController.tabBar.items objectAtIndex:2]; //set objectAtIndex you want (For Instagram its 2)
UIImage *unselectedImage = [UIImage imageNamed:#"unselected-imagename"]; //set unselected image
UIImage *selectedImage = [UIImage imageNamed:#"selected-imagename"]; //set selected image
[tabBarItem setImage: [unselectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem setSelectedImage: selectedImage];
Hope this helps :)
Cheers

How to change UITabBar image dynamically in iOS

At some point during runtime, I want to change the image for one of the tabs in my UITabBar. Here is what I have tried so far:
[[self.tabBarController.tabBar.items objectAtIndex:1]
setImage:[UIImage imageNamed:#"image-name"]
forState:UIControlStateNormal];
The above gives me a -[UITabBarItem setImage:forState:]: unrecognized selector sent to instance
If I use the setImage method without forState it works, but this method was deprecated in iOS 3.
I tried your answers, but now there's this weird blue line above the UITabBar's UIIMage I changed. Any idea why?
Use image and selectedImage properties:
UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:1];
item.image = [UIImage imageNamed:#"image"];
item.selectedImage = [UIImage imageNamed:#"selected_image"];
Also pay attention on this:
By default, the actual selected image is automatically created from
the alpha values in the source image. To prevent system coloring,
provide images with UIImageRenderingModeAlwaysOriginal.
UITabBarItem extends UIBarItem which has an image property.
Do:
[[self.tabBarController.tabBar.items objectAtIndex:1] setImage:[UIImage imageNamed:#"image-name"]];
Though it would be easier to read and debug as follows:
UITabBarItem *item = self.tabBarController.tabBar.items[1];
item.image = [UIImage imageNamed:#"image-name"];
though the question is very old and the answer by #visput absolutely working but for some one complete newbie like me
In your UITabBarController implementation
if while loading of the UITabBarController you want, put it in viewDidLoad method
UITabBarItem *item = [self.tabBar.items objectAtIndex:ITEM_INDEX];// item index is the tab index which starts from 0
item.image = [UIImage imageNamed:#"image"];
item.selectedImage = [UIImage imageNamed:#"image"];
If you want to change it runtime like on selection of something, put this code in viewDidLayoutSubviews method

iOS 7: UITabBarItem badge z-index

I'd like to show a UITabBarItem badge above the selectionIndicatorImage. There are 3 screenshots:
Screenshots
Light gray color is the selectionIndicatorImage. Yes, badge looks good. When I touch up inside at the cloud icon UITabBar become:
It's wrong.. I'd like to show badge above the selection image.
If there is no icon for UITabBar - it looks good.
How can I fix this issue?
Thanks in advance.
Edited
I add icons in the storyboard. For badge I've made the code:
UITabBarItem *cartTabBarItem = (UITabBarItem *)[self.tabBarController.tabBar.items objectAtIndex:3];
if ([[DataSourceWrapper getInstance] getFullCost] == 0)
cartTabBarItem.badgeValue = nil;
else
cartTabBarItem.badgeValue = [NSString stringWithFormat:#"%.0f тнг", [[DataSourceWrapper getInstance] getFullCost]];
For selectionIndicatorImage
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:#"selected-tabbar-bg.png"]];
I know it's a bit tricky, but I think Apple didn't add TabBar subview in correct order.
anyway I've fixed it the following way
for (UIView *subview in marketTrackerAppDelegate.tabBarController.tabBar.subviews)
{
if ([marketTrackerAppDelegate.tabBarController.neededController.tabBarItem respondsToSelector:#selector(view)] &&
[marketTrackerAppDelegate.tabBarController.neededController.tabBarItem performSelector:#selector(view)] == subview)
{
[marketTrackerAppDelegate.tabBarController.tabBar bringSubviewToFront:subview];
break;
}
}
I had the same issue. You can fix it with code that #dollar8 suggested or change order of UITabBarItems creation.
I have 4th tab with badge and SelectionIndicatorImage of 5th tab overlay badge of 4th tab, so first I set 5th tab:
UIImage *item5image = [UIImage imageNamed:#"profile_tabbar_item"];
UIImage *item5imageSel = [UIImage imageNamed:#"profile_tabbar_item_selected"];
item5image = [item5image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item5imageSel = [item5imageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab5.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"" image:item5image selectedImage:item5imageSel];
and after set 4th tab:
UIImage *item4image = [UIImage imageNamed:#"messages_tabbar_item"];
UIImage *item4imageSel = [UIImage imageNamed:#"messages_tabbar_item_selected"];
item4image = [item4image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
item4imageSel = [item4imageSel imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tab4.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"" image:item4image selectedImage:item4imageSel];
And the order of subview will be right.
When I set 4th tab before 5th tab - the SelectionIndicatorImage overlay badge view.

Setting Image To The UITabbar in iphone?

I am new to ios development.
I created UITabbar programatically and set its delegate to self. All functions well. But my tabbar consists three tab bar items. I have given different images to the different tab bar items. But they all shows another image.
This is my code:
UITabbarItem item1 = [[UITabBarItem alloc] initWithTitle:#"item1" image:[UIImage imageNamed:#"imagename"] tag:1];
Try this :
UIImage *selectedImage0 = [UIImage imageNamed:#"tab-selected.png"];
UIImage *unselectedImage0 = [UIImage imageNamed:#"tab-unselected.png"];
[item1 setFinishedSelectedImage:selectedImage0 withFinishedUnselectedImage:unselectedImage0];

Resources