iOS programmatically change tab bar item images on the fly not working

3.5k views Asked by At

I have a problem when trying to set images on my tab bar programmatically.

I want to have all tabs with a black background and white icon with white text. And when you browse through them the tab that is selected changes so that it has a red background with a white icon and title. Right now I have the same image on all tabs for testing purposes. But they will be all different images.

Now my problem is, that it seems that this code is ignored and the "original" (with settings from storyboard) appears. Nothing changes, except when I change values in the storyboard.

I try to set them in didFinishLaunchingWithOptions in my AppDelegate file as such:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UITabBarController *tc = [mainStoryboard instantiateViewControllerWithIdentifier:@"tabbar"];

UITabBar *tabBar = tc.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:@"ikon_clubkort"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.image = [[UIImage imageNamed:@"ikon_clubkort"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem1.title = @"xxxx";

tabBarItem2.selectedImage = [[UIImage imageNamed:@"ikon_clubkort"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.image = [[UIImage imageNamed:@"ikon_clubkort"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem2.title = @"xxxx";

tabBarItem3.selectedImage = [[UIImage imageNamed:@"ikon_clubkort"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.image = [[UIImage imageNamed:@"ikon_clubkort"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem3.title = @"xxxx";

tabBarItem4.selectedImage = [[UIImage imageNamed:@"ikon_clubkort"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.image = [[UIImage imageNamed:@"ikon_clubkort"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem4.title = @"xxxx";

tabBarItem5.selectedImage = [[UIImage imageNamed:@"ikon_clubkort"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem5.image = [[UIImage imageNamed:@"ikon_clubkort"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
tabBarItem5.title = @"xxxx";

return YES; 
}

Any idea what I am doing wrong?

1

There are 1 answers

4
remingtonspaz On BEST ANSWER

From what I read, basically you are creating a Tab Bar Controller programmatically but not displaying it.

You can do 2 things:

  1. Set the created Tab Bar Controller as the delegate's root view controller, by putting this at the end of application:didFinishLaunchingWithOptions :

    self.window.rootViewController = tc

  2. Create a subclass of UITabBarController, put the tab bar item setting code that you had on the new class' viewDidLoad method, and assign the class to the view controller in the storyboard.