【问题标题】:Changing text color of uitabbaritem更改 uitabbaritem 的文本颜色
【发布时间】:2011-03-04 09:03:14
【问题描述】:

有什么方法可以将uitabbar 项目的文本颜色从默认灰色更改为白色,并将所选颜色更改为蓝色?

【问题讨论】:

    标签: iphone


    【解决方案1】:

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

    【讨论】:

    • 非常感谢您的快速回复,但这对我没有帮助,因为我可以更改图像颜色,并且此代码也是如此。请建议我如何更改 uitabbaritem 的文本颜色.
    • 这个问题是更改标签栏项目上的图像,我需要更改标签 baritem 上的文本颜色
    【解决方案2】:

    UITabBarItem 几乎是不可自定义的,所以如果你必须,你可以:

    1. 通过迭代UITabBar 的子视图,使用-[NSObject isKindOfClass:] 找到标签并更改它们的颜色。

    2. 创建您自己的UITabBar 并滚动自定义标签栏项目。

    3. 试试 Three20 的 TTTabBar 等替代方案。

    【讨论】:

    • 我应该在子视图中寻找哪个 obj?!我登录并看到有一堆 'UITabBarButton' ,但是不知道如何使用该类!?
    • 我最终为记录创建了一个自定义标签栏视图。
    【解决方案3】:

    编辑:由于 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 即可。

    【讨论】:

      【解决方案4】:

      老问题,但我有一个 iOS 5 及更高版本支持的新答案(我也在使用 LLVM 4.0 文字)

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

      【讨论】:

      • + 用于更新新的最佳实践。
      • 同意 Detartrate 的评论,因为你什么时候可以使用 @{} 创建字典......太棒了。非常 JSONesque。
      • 从 LLVM 4.0 编译器开始。查看文档以了解您可以使用文字执行的所有其他操作:clang.llvm.org/docs/ObjectiveCLiterals.html。您还可以将当前项目转换为新的文字语法:XCode > Edit > Refactor > Convert to Modern Objective-C Syntax ...。我想说它已经存在了大约一年。
      • 这似乎是个错误。你确定应该是UIControlStateHighlighted,而不是UIControlStateSelected
      • 这是我使用并且最喜欢的解决方案;但是, UITextAttributeTextColor 已被弃用,因此只需将其替换为 NSForegroundColorAttributeName ,代码应该可以无错误地编译。
      【解决方案5】:

      UITextAttributeTextColor 在 iOS 7 中已弃用。请改用 NSForegroundColorAttributeName

      [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] }
                                                   forState:UIControlStateNormal];
      

      在 Swift 中

      UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.blackColor()], forState: .Normal)
      

      【讨论】:

        【解决方案6】:

        要同时设置 2 个UIControlState 的颜色,您可以使用union

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

        【讨论】:

          【解决方案7】:

          可能对你有帮助

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

          【讨论】:

            【解决方案8】:

            Swift3

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

            【讨论】:

              【解决方案9】:

              保持简单!

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

              【讨论】:

              • 欢迎来到 StackOverflow。其中只有代码的答案往往会被标记为删除,因为它们是“低质量”的。请阅读有关回答问题的帮助部分,然后考虑在您的回答中添加一些评论。
              【解决方案10】:

              从 iOS 10 开始,可以在 UITabBar 上设置 unselectedItemTintColor

              UITabBar 中的tintColor 比 selectedItem 的颜色。

              如果您想为任何项目设置唯一值,您还可以直接在项目上设置tabBarItem.titleTextAttributes(for:)(前面提到)与tabBarItem.imagetabBarItem.selectedImage 结合使用。

              【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-08-24
              • 2012-01-14
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多