Reputation: 239
Is there any way to change the text color of a uitabbar
item from default gray to white and the selected color to blue?
Upvotes: 23
Views: 30192
Reputation: 3660
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
.
Upvotes: 5
Reputation: 1557
Keep it simple!
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
Upvotes: -1
Reputation: 1875
Swift3
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.brown], for: .normal)
Upvotes: 0
Reputation: 1112
It may helps you
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Selected)
Upvotes: 2
Reputation: 8298
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))
Upvotes: 2
Reputation: 26972
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];
Upvotes: 78
Reputation: 2431
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)
Upvotes: 14
Reputation: 5397
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.
Upvotes: 2
Reputation: 3200
UITabBarItem is pretty much non-customizable so if you must, you could:
Piggyback by iterating thru the UITabBar
’s subviews, find the labels using -[NSObject isKindOfClass:]
and change their color.
Create your own UITabBar
and roll custom tab bar items.
Try alternatives like Three20’s TTTabBar
.
Upvotes: 1
Reputation: 37504
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.
Upvotes: 0