2010-06-16 101 views

回答

0

查看this questionthis question的回答,但请注意,您的应用可能因为修改默认的tabbar组件而被拒绝。

+0

非常感谢您的快速响应,但这并不能帮助我,因为我可以更改图像颜色,并且此代码也是如此。请向我建议如何更改uitabbaritem的文本颜色。 – yogendra 2010-06-16 09:25:22

+0

这个问题正在改变标签栏项目上的图像,我需要更改标签baritem上的文本颜色 – 2012-06-08 08:30:27

1

UITabBarItem是相当多的非定制的,所以如果你一定要,你可以:

  1. 肩扛通过迭代通的UITabBar的子视图,使用-[NSObject isKindOfClass:]找到标签和改变自己的颜色。

  2. 创建自己的UITabBar并滚动自定义选项卡栏项目。

  3. 尝试类似Three20的替代方案TTTabBar

+0

哪些obj应该在子视图内寻找?我登录,我看到有一堆'UITabBarButton',但不知道如何使用该类!? – 2011-05-18 22:10:17

+0

我最终创建了记录的自定义标签栏视图。 – 2011-05-19 08:19:12

2

编辑:下面是不再最佳做法,因为新的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重置为零即可。

74

老问题,但我有

[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] } 
             forState:UIControlStateNormal]; 
[[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] } 
             forState:UIControlStateSelected]; 
+0

+用新的最佳做法进行更新。 – DetartrateD 2012-09-24 17:48:09

+0

同意Detartrate的评论,因为你什么时候可以使用@ {} ...创建一个字典Awesomeness。非常JSONesque。 – 2013-04-16 18:57:01

+0

由于LLVM 4.0编译器。查看文档以查看可以使用文字处理的所有其他内容:http://clang.llvm.org/docs/ObjectiveCLiterals.html。您还可以将当前项目转换为新的字面语法:XCode> Edit> Refactor>转换为现代Objective-C语法....现在已经存在了大约一年,现在我会说。 – bandejapaisa 2013-04-16 21:22:51

11

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) 
0

设置颜色为2 UIControlState一次可以使用union

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.redColor()], forState: UIControlState.Selected.union(UIControlState.Highlighted)) 
2

它可以帮助你

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.whiteColor()], forState: .Selected) 
1

Swift3

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.brown], for: .normal) 
0

保持简单!

[[UITabBar appearance] setTintColor:[UIColor blackColor]]; 
+0

这不提供问题的答案。一旦你有足够的[声誉](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-c​​an- I-DO-代替)。 - [来自评论](/ review/low-quality-posts/18808729) – 2018-02-13 17:01:37

+0

欢迎来到StackOverflow。只有代码在他们的答案往往会被标记为删除,因为他们是“低质量”。请阅读关于回答问题的帮助部分,然后考虑在答案中添加一些评论。 – Graham 2018-02-14 02:05:19

0

由于iOS的10也能够设置unselectedItemTintColorUITabBar

UITabBartintColor是比selectedItem的颜色。

如果你想去唯一值的任何项目,您还可以直接与tabBarItem.imagetabBarItem.selectedImage设置tabBarItem.titleTextAttributes(for:)(前面提到的)也对项目组合。