【问题标题】:How can I change the text and icon colors for tabBarItems in iOS 7?如何在 iOS 7 中更改 tabBarItems 的文本和图标颜色?
【发布时间】:2013-09-15 02:24:17
【问题描述】:

如何在 iOS 7 中更改 UITabBar 和 UITabBarItems 的文本和图标颜色?对于未选择的标签栏项目,默认的灰色文本看起来很暗,难以阅读。

【问题讨论】:

    标签: uitabbarcontroller uitabbar tabbar uitabbaritem ios7


    【解决方案1】:

    为此,您需要做两件事:

    1)如果要自定义TabBar本身,需要为tabBarController设置barTintColor:

        // this will generate a black tab bar
        tabBarController.tabBar.barTintColor = [UIColor blackColor];
    
        // this will give selected icons and text your apps tint color
        tabBarController.tabBar.tintColor = appTintColor;  // appTintColor is a UIColor *
    

    2) 为要覆盖的每个状态设置 tabBarItem 文本外观:

    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                        NSForegroundColorAttributeName : appTintColor
                                                        } forState:UIControlStateSelected];
    
    
    // doing this results in an easier to read unselected state then the default iOS 7 one
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f],
                                                        NSForegroundColorAttributeName : [UIColor colorWithRed:.5 green:.5 blue:.5 alpha:1]
                                                        } forState:UIControlStateNormal];
    

    【讨论】:

    • Ed 几乎涵盖了您在这里需要的所有内容,不幸的是,似乎没有办法调整其他选项卡的颜色。到目前为止,唯一的选择是使用图片。
    • 如何设置未选中的图标颜色?
    • 这不会改变未选中项的颜色。唯一的方法似乎是使用如下图像。
    • 10 号字体?有点小?
    • @Pwner 我也想取消选中图标颜色,我发现使用正确的图像,并将其渲染为原始模式是一种方法。如果您发现任何其他方法,请告诉我。
    【解决方案2】:

    使用self.tabBarController.tabBar.barStyle = UIBarStyleBlack; 使标签栏变黑

    【讨论】:

      【解决方案3】:

      Ed 的回答很完美,但让我补充一点。 TabBar 默认是半透明的,因此会受到 TabBar 下视图颜色的影响(即每个成员 viewController 的视图颜色都会影响 TabBar 的外观)。

      所以我设置下面的代码不受影响。

      self.tabBarController.tabBar.translucent = false;
      

      这里连同 Ed 的回答是我现在使用的完整代码。

      self.tabBarController.tabBar.barTintColor = [UIColor blackColor];
      self.tabBarController.tabBar.translucent = false;
      self.tabBarController.tabBar.tintColor = [UIColor blueColor];
      

      【讨论】:

        【解决方案4】:

        这对我有用,可以为标签栏中的非活动项目着色

        UITabBarItem *item = [self.tabBar.items objectAtIndex:1];
        

        //这里你需要使用你想要的颜色的图标,因为它会被渲染成原来的样子

        item.image = [[UIImage imageNamed:@"unselected.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        

        // 此图标用于选定的选项卡,它将按照

        中的定义进行着色
        self.tabBar.tintColor
        item.selectedImage = [UIImage imageNamed:@"selected.png"];
        

        【讨论】:

        • 这似乎是改变未选中标签颜色的唯一方法。
        • 使用imageWithRenderingMode是改变标签图片颜色的关键
        • 很棒的解决方案,也是唯一有效的解决方案......顺便说一句,还有什么方法可以更改标签项的标题颜色?
        【解决方案5】:

        你试试

        for (UITabBarItem *item in self.tabBarController.tabBar.items) {
                item.image = [[UIImage imageNamed:@"youimage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
                [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                   [UIColor whiteColor], UITextAttributeTextColor,
                                                                   nil] forState:UIControlStateNormal];
        
                item.selectedImage = [UIImage imageNamed:@"youimage.png"];
            }
        

        【讨论】:

        • UITextAttributeTextColor 在 iOS7 中已弃用
        【解决方案6】:
        [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                   [UIColor whiteColor], UITextAttributeTextColor,
                                                                   nil] 
        

        【讨论】:

          【解决方案7】:

          这应该也适用于 iOS 8

          对于未选中的标签栏项目:

          [[UIView appearanceWhenContainedIn:[UITabBar class], nil] setTintColor: [UIColor whiteColor]];
          

          对于选定的标签栏项目:

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

          【讨论】:

          • 标签切换时颜色会改变
          • 那么不要使用[[UITabBar外观] setTintColor:[UIColor orangeColor]];
          • [tabBarItem1 setFinishedSelectedImage:[[UIImage imageNamed:@"mytrip_tab_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] withFinishedUnselectedImage:[[UIImage imageNamed:@"mytrip_tab"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
          • 当有两张不同颜色的相同图像时,上述注释将起作用。我们可以通过选择和取消选择UITabbar项目来呈现。
          • @Usharao 未选中图标的代码不错,但我有一个特定要求,即只有一个选项卡项应该有不同的颜色。
          【解决方案8】:

          在 iOS 8 中测试了永久文本颜色(选中/未选中)和图像颜色(选中/未选中)没有为每个选项卡创建两个具有不同颜色的图像:

          文字颜色:

          [[UITabBar appearance] setTintColor: selectedTabColor ];
              [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                 **yourFont**, NSFontAttributeName,
                                                                 ** selectedTabColor**, NSForegroundColorAttributeName,
                                                                 nil] forState:UIControlStateNormal];
          
              [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                 **yourFont**, NSFontAttributeName,
                                                                 **selectedTabColor**, NSForegroundColorAttributeName,
                                                                 nil] forState:UIControlStateSelected];
          

          图像颜色:(假设原始图像具有您希望显示为未选中的颜色)

          UITabBarController 子类 -awakeFromNib 中:

              for (int i =0; i<self.viewControllers.count; i++)
              {
                  UITabBarItem *tab = [self.tabBar.items objectAtIndex:i];
                  tab.image = [tab.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
              }
          

          致谢:整个互联网和堆栈溢出 XD

          【讨论】:

          • 谢谢!你可能知道如何在这张图片上添加分隔符吗? dropbox.com/s/0u6jk2yv4kugfpg/…
          • 对于那个你可能需要子类标签栏或标签栏项目
          【解决方案9】:

          在标签栏中更改文本颜色的无代码方式:

          如果您只是使用 iOS 10,那么您可以更改标签栏中的图像色调

          如果您还支持 iOS 9 及更低版本,那么您还必须将 tintColor 添加到每个选项卡栏项中的用户定义器运行时属性

          如果您还想更改图标颜色,请确保您的资产文件夹中包含正确的颜色图像并将渲染更改为原始图像

          【讨论】:

            【解决方案10】:

            @Usharao 上面的回答对我有用;

            我的问题是在启动时我的所有 TabBarItems 似乎处于选定状态,都具有相同的“蓝色”着色。 通过一一选择所有选项卡,颜色状态将得到纠正。

            我在 AppDelegate 类中使用了以下代码:(兼容 >= IOS9)

            [[UIView appearanceWhenContainedInInstancesOfClasses:@[[UITabBar class]]] 
                                                    setTintColor:[UIColor lightGrayColor]];
            

            【讨论】:

              【解决方案11】:

              现在iOS10可以使用了

              @property (nonatomic, readwrite, copy, nullable) UIColor *unselectedItemTintColor
              

              更改TabBarItem 未选中状态下图像和文本的默认颜色。

              因此,tintColorunselectedItemTintColor 这对属性让我们可以完全控制项目的颜色。

              【讨论】:

                猜你喜欢
                • 2014-01-01
                • 2013-10-14
                • 1970-01-01
                • 1970-01-01
                • 2013-10-04
                • 2021-08-22
                • 2014-03-03
                • 2015-09-15
                • 2021-08-08
                相关资源
                最近更新 更多