Changing text color of uitabbaritem
Is there any way to change th开发者_StackOverflow中文版e text color of a uitabbar
item from default gray to white and the selected color to blue?
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];
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)
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
.
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.
It may helps you
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Selected)
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
.
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))
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.
Swift3
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.brown], for: .normal)
Keep it simple!
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
精彩评论