The icons show fine in ios 6 but not in ios 7. I'm setting the selected state in the viewController viewDidLoad method. When the user selects a tab bar item the image disappears. Here is my code:
UITabBar *tabBar = self.tabBarController.tabBar;
if ([UITabBar instancesRespondToSelector:@selector(setSelectedImageTintColor:)]) {
[self.tabBarController.tabBar setSelectedImageTintColor:[UIColor whiteColor]];
}
UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
[item0 setTitle:@"Home"];
[item1 setTitle:@"Calendar"];
[item2 setTitle:@"News"];
[item3 setTitle:@"My Events"];
[item0 setFinishedSelectedImage:[UIImage imageNamed:@"homeIconSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"home2.png"]];
[item1 setFinishedSelectedImage:[UIImage imageNamed:@"Calendar"] withFinishedUnselectedImage:[UIImage imageNamed:@"CalendarIconSelected"]];
[item2 setFinishedSelectedImage:[UIImage imageNamed:@"NewsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:@"News"]];
[item3 setFinishedSelectedImage:[UIImage imageNamed:@"EventsIconSelected"] withFinishedUnselectedImage:[UIImage imageNamed:@"Events"]];
[item1 imageInsets];
[item2 imageInsets];
[item3 imageInsets];
No answers helped fixing this issue. The main reason is that my
TabBarController
wasn't myRootViewController
.The solution I used for Storyboards, and I just clicked my
UITabButton
and I added a runtime attribute forselectedImage
:For each of the different views associated with the
UITabController
.After spending a couple of hours trying to make my custom tabbar to work for both iOS 6 & 7, that's what worked for me...
It is easy and clean solution of category for UITabBar Items.
Just create category and use the Runtime Attribute and refer it from category like below.
Old questions, but going to add my solution here as well for Xamarin.iOS/C# and those who want to set the images via Interface builder. I set the
Selected Image
andImage
attributes via Interface Builder. Then in code I defined anInitTabs()
method like this:Call
InitTabs()
inViewDidLoad
and now the proper image will appear for both the selected and unselected state.Set bar item image's render mode to original can solve this problem. This can be done by using images in the .xcassets, so you don't have to write lots of codes.
First step, drap&drop your bar item images to Assets.xcassets.
Second step, choose the bar item image, and change [Render As] to [Original Image]
ps: I usually set the TabBarController's tab bar items all by story board to avoid writing lots of code.
if you are working with storyboards you have to put the identifier : "custom" in Navigation Controller.
then :
This works for me.