23

Is there any way to change the text color of a uitabbar item from default gray to white and the selected color to blue?

BDL
  • 21,052
  • 22
  • 49
  • 55
yogendra
  • 239
  • 1
  • 2
  • 3

10 Answers10

77

Old question, but I have a new answer that is supported in iOS 5 onwards (also I'm using LLVM 4.0 literals)

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
                                         forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] }
                                         forState:UIControlStateSelected];
bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
  • + for updating with new best practice. – DetartrateD Sep 24 '12 at 17:48
  • agreed with Detartrate's comment, since when can you create a dictionary using @{}... Awesomeness. Very JSONesque. – Brad G Apr 16 '13 at 18:57
  • Since LLVM 4.0 compiler. Check out the documents to see all the other things you can do with literals: http://clang.llvm.org/docs/ObjectiveCLiterals.html. You can also convert your current project to the new literal syntax: XCode > Edit > Refactor > Convert to Modern Objective-C Syntax .... It's been around for about a year now I'd say. – bandejapaisa Apr 16 '13 at 21:22
  • It seems like mistake. Are you sure, that it should be `UIControlStateHighlighted`, not `UIControlStateSelected`? – skywinder May 15 '14 at 07:19
  • 2
    This was the solution I use and like best; however, UITextAttributeTextColor is deprecated so just replace that with NSForegroundColorAttributeName and the code should compile error-free. – UXUiOS Jul 30 '14 at 03:50
14

UITextAttributeTextColor is deprecated from iOS 7. Use NSForegroundColorAttributeName instead.

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
                                             forState:UIControlStateNormal];

And in Swift

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.blackColor()], forState: .Normal)
Eike
  • 2,311
  • 20
  • 31
5

Since iOS 10 it is possible to set the unselectedItemTintColor on the UITabBar.

The tintColor of the UITabBar is than the color for the selectedItem.

If you want to go to unique values for any item you can also set the tabBarItem.titleTextAttributes(for:) (mentioned earlier) also on the item directly in combination with tabBarItem.image and tabBarItem.selectedImage.

Saren Inden
  • 3,450
  • 4
  • 32
  • 45
  • 1
    This one is really hard to find. This should be the accepted answer. Here is the link for the highly hidden documentation of this property: https://developer.apple.com/documentation/uikit/uitabbar/1648949-unselecteditemtintcolor – vicegax Jun 17 '20 at 14:02
2

To set the color for 2 UIControlState at once you can use union:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.redColor()], forState: UIControlState.Selected.union(UIControlState.Highlighted))
Roland Keesom
  • 8,180
  • 5
  • 45
  • 52
2

It may helps you

 UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Selected)
Amit Jagesha シ
  • 1,092
  • 12
  • 21
2

EDIT: the following is no longer best practice since new APIs were added to the iOS SDK

Subclass UITabBarController (as CustomTabBarController in this example) and put the following code in your .m implementation file:

@interface CustomTabBarController()

@property (nonatomic, retain) NSArray *tabTitleLabels;

@end


@implementation CustomTabBarController

@synthesize tabTitleLabels;

- (NSArray *)tabTitleLabels
{
    // Check if we need to update the tab labels 
    if ([tabTitleLabels count] != [self.viewControllers count])
        self.tabTitleLabels = nil;

    // Create custom tab bar title labels
    if (!tabTitleLabels)
    {
        tabTitleLabels = [[NSMutableArray alloc] init];

        for (UIView *view in self.tabBar.subviews)
        {      
            if ([NSStringFromClass([view class]) isEqualToString:@"UITabBarButton"])
            {
                for (UIView *subview in view.subviews)
                {                                    
                    if ([subview isKindOfClass:[UILabel class]])
                    {
                        UILabel *label = (UILabel *)subview;

                        UILabel *newLabel = [[UILabel alloc] init];
                        newLabel.font = label.font;
                        newLabel.text = label.text;
                        newLabel.backgroundColor = label.backgroundColor;
                        newLabel.opaque = YES;
                        newLabel.frame = CGRectMake(0, 0, label.frame.size.width, label.frame.size.height -1);    
                        [subview addSubview:newLabel];

                        [((NSMutableArray *)tabTitleLabels) addObject:newLabel];
                        [newLabel release];
                    }
                }
            }
        }      
    }

    return tabTitleLabels;
}

// Customize the desired colors here
- (void)recolorTabBarTitleLabels
{
    for (UILabel *label in self.tabTitleLabels)
    {
        label.textColor = [UIColor whiteColor];
        label.backgroundColor = [UIColor blackColor];
    }
    UILabel *selectedLabel = [self.tabTitleLabels objectAtIndex:self.selectedIndex];
    selectedLabel.textColor = [UIColor blueColor];            
    selectedLabel.backgroundColor = [UIColor colorWithWhite:.15 alpha:1];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self recolorTabBarTitleLabels];
}

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController 
{   
    [self recolorTabBarTitleLabels];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.tabTitleLabels = nil;
}

- (void)dealloc
{
    [tabTitleLabels release];
    [super dealloc];
}

@end

This may be a year late, but I hope my code saves someone some work!

Note: it is not designed to support switching in/out new tab bar items, though you would just have to reset tabTitleLabels to nil to do so.

Ilias Karim
  • 4,798
  • 3
  • 38
  • 60
1

UITabBarItem is pretty much non-customizable so if you must, you could:

  1. Piggyback by iterating thru the UITabBar’s subviews, find the labels using -[NSObject isKindOfClass:] and change their color.

  2. Create your own UITabBar and roll custom tab bar items.

  3. Try alternatives like Three20’s TTTabBar.

Evadne Wu
  • 3,190
  • 1
  • 25
  • 25
1

Swift3

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.brown], for: .normal) 
Brett
  • 2,635
  • 22
  • 35
Jazzmine
  • 1,837
  • 8
  • 36
  • 54
0

Check out this question and this question's answers, but be aware that your app might get rejected for modifying the default tabbar components like that.

Community
  • 1
  • 1
luvieere
  • 37,065
  • 18
  • 127
  • 179
  • thanks a lot for your quick response but this does not helps me as I am able to change the images color and this code does the same..please suggest me how to change the text color of the uitabbaritem. – yogendra Jun 16 '10 at 09:25
  • This question was changing the image on the tab bar item, i need to change the text color on the tab baritem – Vineesh TP Jun 08 '12 at 08:30
0

Keep it simple!

[[UITabBar appearance] setTintColor:[UIColor blackColor]];
thus
  • 1,326
  • 1
  • 14
  • 23
  • 1
    Welcome to StackOverflow. Answers with only code in them tend to get flagged for deletion as they are "low quality". Please read the help section on answering questions then consider adding some commentary to your Answer. – Graham Feb 14 '18 at 02:05