0

I am trying to set the icons of a UITabBarItem, but it is not working. By the way, I'm using Xcode 5 Beta for this project.

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    {
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
        UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
        [[UITabBar appearance] setBackgroundImage:tabBarBackground];
        [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selected.png"]];
        [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
    }
    return YES;
}

enter image description here

I am trying to get the icons to be white when both selected and unselected, but they are remaining gray in the unselected state.


Edit

I tried doing this, but now im getting the error "Expression Result Unused"

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
        UITabBar *tabBar = tabBarController.tabBar;
        UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
        tabBarItem1.title = @"Home";
        UIImage *graph = [UIImage imageNamed:@"graph.png"];
        [tabBarItem1 initWithTitle:(NSString *)@"HELLO" image:(UIImage *)graph selectedImage:(UIImage *)graph];
Community
  • 1
  • 1
i_duhh_bomb
  • 300
  • 1
  • 10

1 Answers1

0

tintColor sets the tint color of the background, you can't see this if you use a custom background image. If you don't want to have the system default gradient you have to use setFinishedSelectedImage:withFinishedUnselectedImage: and set your white image for both of them.

Like this:

UIImage *selectedImage = ...
UIImage *unselectedImage = selectedImage;
[tabBarItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unselectedImage];

If you are setting those images in a Storyboard you can iterate over the existing UITabBarItems and change their images.

Put something like this into application:didFinishLaunchingWithOptions:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
NSAssert([tabBarController isKindOfClass:[UITabBarController class]], @"self.window.rootViewController must be a tabBarController");
for (UIViewController *viewController in tabBarController.viewControllers) {
    UITabBarItem *tabBarItem = viewController.tabBarItem;
    UIImage *tabImage = tabBarItem.image;
    [tabBarItem setFinishedSelectedImage:tabImage withFinishedUnselectedImage:tabImage];
}
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247