【发布时间】:2011-03-04 09:03:14
【问题描述】:
有什么方法可以将uitabbar 项目的文本颜色从默认灰色更改为白色,并将所选颜色更改为蓝色?
【问题讨论】:
标签: iphone
有什么方法可以将uitabbar 项目的文本颜色从默认灰色更改为白色,并将所选颜色更改为蓝色?
【问题讨论】:
标签: iphone
查看this question 和this question 的答案,但请注意,您的应用可能会因为修改默认标签栏组件而被拒绝。
【讨论】:
UITabBarItem 几乎是不可自定义的,所以如果你必须,你可以:
通过迭代UITabBar 的子视图,使用-[NSObject isKindOfClass:] 找到标签并更改它们的颜色。
创建您自己的UITabBar 并滚动自定义标签栏项目。
试试 Three20 的 TTTabBar 等替代方案。
【讨论】:
编辑:由于 iOS SDK 中添加了新的 API,因此以下不再是最佳做法
子类 UITabBarController(在本例中为 CustomTabBarController)并将以下代码放入您的 .m 实现文件中:
@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
这可能晚了一年,但我希望我的代码可以为某人节省一些工作!
注意:它不支持切换新标签栏项目,但您只需将 tabTitleLabels 重置为 nil 即可。
【讨论】:
老问题,但我有一个 iOS 5 及更高版本支持的新答案(我也在使用 LLVM 4.0 文字)
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] }
forState:UIControlStateSelected];
【讨论】:
UIControlStateHighlighted,而不是UIControlStateSelected?
UITextAttributeTextColor 在 iOS 7 中已弃用。请改用 NSForegroundColorAttributeName。
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
forState:UIControlStateNormal];
在 Swift 中
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.blackColor()], forState: .Normal)
【讨论】:
要同时设置 2 个UIControlState 的颜色,您可以使用union:
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.redColor()], forState: UIControlState.Selected.union(UIControlState.Highlighted))
【讨论】:
可能对你有帮助
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Selected)
【讨论】:
Swift3
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.brown], for: .normal)
【讨论】:
保持简单!
[[UITabBar appearance] setTintColor:[UIColor blackColor]];
【讨论】:
从 iOS 10 开始,可以在 UITabBar 上设置 unselectedItemTintColor。
UITabBar 中的tintColor 比 selectedItem 的颜色。
如果您想为任何项目设置唯一值,您还可以直接在项目上设置tabBarItem.titleTextAttributes(for:)(前面提到)与tabBarItem.image 和tabBarItem.selectedImage 结合使用。
【讨论】: