0

I would like to change the icons of my UITabBar. Yes there are several threads on StackOverFlow, I read a huge number of them but I didn't find my answer.

My first View Controller is a Login View Controller and my second VC is a TabBarViewController. I named TheTabBar the TabBarViewController in StoryBoard.

Here is my code but it didn't works and I don't know why :

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController* tabBarController = (UITabBarController*)[storyboard instantiateViewControllerWithIdentifier:@"TheTabBar"];

NSLog(@"STORY = %@",storyboard);
NSLog(@"TABBAR = %@",tabBarController);

UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];

tabBarItem1.selectedImage = [[UIImage imageNamed:@"firstselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:@"firstnonselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.title = @"First";

tabBarItem2.selectedImage = [[UIImage imageNamed:@"secondselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:@"secondnonselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.title = @"Second";

tabBarItem3.selectedImage = [[UIImage imageNamed:@"thirdselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:@"thirdnonselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.title = @"Third";

tabBarItem4.selectedImage = [[UIImage imageNamed:@"fourselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.image = [[UIImage imageNamed:@"fournonselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.title = @"Four";

tabBarItem5.selectedImage = [[UIImage imageNamed:@"fiveselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem5.image = [[UIImage imageNamed:@"fivenonselected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem5.title = @"Five";

Thanks everybody !

Michel
  • 100
  • 10
  • Please can you add more information: which class/method is the above code from? How do you show the tabBarViewController from the login view controller? – pbasdf Nov 25 '14 at 22:47
  • Hi ! Thanks for your help. I was trying different solution and I just found the solution here http://stackoverflow.com/questions/19426251/selected-state-of-tab-bar-icon-in-ios-7 when you wrote your comment. Thanks – Michel Nov 25 '14 at 22:52

2 Answers2

4

If you use StoryBoard, you can use "User Defined Runtime Attributes" to change UITabBarItem's selected image.

it look like this: enter image description here

"selected image" Attributes in Attributes Inspector did not work, it is Xcode's bug.

You can use Image Set to set RenderingMode(Render As) of image: enter image description here

Mornirch
  • 1,134
  • 9
  • 10
  • That bit regarding the Asset Catalog worked for me (setting **Render As** to **Original Image**). – Clifton Labrum Jun 18 '15 at 23:48
  • render as original image solves the trick.....Mornich you are genious....thanks!!! i spend a lot of time trying to figure this out..... – IamMashed Nov 01 '15 at 13:50
0

You can declare UITabBar in AppDelegate file of your application. Then assign your tab bar in controller to AppDelegate's tab bar object. Then you can changes icon of tabbar using app delegate's tabbar object from any controller.

Jayeshkumar Sojitra
  • 2,231
  • 4
  • 29
  • 42