任何人都可以帮助我在这个奇怪的问题是有反正uitabbar项目的文本颜色从默认灰色更改为白色和选定的颜色蓝色。改变uitabbaritem的文字颜色
回答
查看this question和this question的回答,但请注意,您的应用可能因为修改默认的tabbar组件而被拒绝。
UITabBarItem是相当多的非定制的,所以如果你一定要,你可以:
肩扛通过迭代通的
UITabBar
的子视图,使用-[NSObject isKindOfClass:]
找到标签和改变自己的颜色。创建自己的
UITabBar
并滚动自定义选项卡栏项目。尝试类似Three20的替代方案
TTTabBar
。
哪些obj应该在子视图内寻找?我登录,我看到有一堆'UITabBarButton',但不知道如何使用该类!? – 2011-05-18 22:10:17
我最终创建了记录的自定义标签栏视图。 – 2011-05-19 08:19:12
编辑:下面是不再最佳做法,因为新的API被添加到了iOS SDK
子类的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重置为零即可。
老问题,但我有
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }
forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] }
forState:UIControlStateSelected];
+用新的最佳做法进行更新。 – DetartrateD 2012-09-24 17:48:09
同意Detartrate的评论,因为你什么时候可以使用@ {} ...创建一个字典Awesomeness。非常JSONesque。 – 2013-04-16 18:57:01
由于LLVM 4.0编译器。查看文档以查看可以使用文字处理的所有其他内容:http://clang.llvm.org/docs/ObjectiveCLiterals.html。您还可以将当前项目转换为新的字面语法:XCode> Edit> Refactor>转换为现代Objective-C语法....现在已经存在了大约一年,现在我会说。 – bandejapaisa 2013-04-16 21:22:51
UITextAttributeTextColor从iOS版7.使用NSForegroundColorAttributeName不提倡使用的是iOS 5中支持起(也我使用LLVM 4.0文本)的新答案代替。
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
forState:UIControlStateNormal];
而且在斯威夫特
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]];
这不提供问题的答案。一旦你有足够的[声誉](https://stackoverflow.com/help/whats-reputation),你将可以[对任何帖子发表评论](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提问者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/18808729) – 2018-02-13 17:01:37
欢迎来到StackOverflow。只有代码在他们的答案往往会被标记为删除,因为他们是“低质量”。请阅读关于回答问题的帮助部分,然后考虑在答案中添加一些评论。 – Graham 2018-02-14 02:05:19
由于iOS的10也能够设置unselectedItemTintColor
上UITabBar
。
UITabBar
的tintColor
是比selectedItem的颜色。
如果你想去唯一值的任何项目,您还可以直接与tabBarItem.image
和tabBarItem.selectedImage
设置tabBarItem.titleTextAttributes(for:)
(前面提到的)也对项目组合。
- 1. 如何改变颜色的文字在UITabBarItem在iOS 4的
- 2. swift UITabbaritem颜色
- 3. 更改Swift中UITabBarItem的背景颜色
- 4. 改变颜色的颜色
- 5. 文字RecyclerView TextView的颜色改变
- 6. 更改文字的颜色,无边框颜色变化
- 7. 如何修改UITabBarItem中文本的颜色?
- 8. 点击jquery改变文字颜色
- 9. 状态栏文字颜色不改变
- 10. 随着时间改变文字颜色
- 11. Javascript功能改变文字颜色
- 12. 初始屏幕文字改变颜色
- 13. 如何在uitabbarcontroller中更改默认的uitabbaritem的灰色颜色?
- 14. 如何改变文字颜色股利不改变边框颜色
- 15. TextView改变背景色的文字颜色变化
- 16. 更改UITabBarItem未选中的颜色色调 - Swift
- 17. 在iOS 10中更改UITabBarItem的选定色调颜色
- 18. 颜色改变文本
- 19. 改变颜色
- 20. 改变颜色
- 21. 改变颜色
- 22. 改变颜色
- 23. 改变颜色
- 24. 改变颜色
- 25. 改变颜色
- 26. 改变颜色
- 27. 改变颜色
- 28. 如何选择更改文本颜色UITabBarItem?
- 29. 改变原子的颜色的颜色
- 30. jquery - 更改文字颜色
非常感谢您的快速响应,但这并不能帮助我,因为我可以更改图像颜色,并且此代码也是如此。请向我建议如何更改uitabbaritem的文本颜色。 – yogendra 2010-06-16 09:25:22
这个问题正在改变标签栏项目上的图像,我需要更改标签baritem上的文本颜色 – 2012-06-08 08:30:27