【问题标题】:Custom tab bar icon colors自定义标签栏图标颜色
【发布时间】:2025-11-25 12:15:01
【问题描述】:

我目前正在使用 Xcode 5 开发一个面向列表的应用程序。我有选项卡栏的自定义色调,选项卡图标的自定义图像,选项卡栏图标图像的自定义色调,当它被选中时,但我找不到如何自定义图标图像在未选择时的色调。现在它只是默认的灰色,与我的绿色标签栏相比你几乎看不到。我想将标签栏图标的图像和名称设为白色。

有人知道如何在 Xcode 5 中设置标签栏图标的图像色调吗?

【问题讨论】:

    标签: ios xcode xcode5


    【解决方案1】:

    你可以试试这个来为选中的图标着色:

    // Custom the tab bar
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
    

    这会为非活动图标着色:

    [self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]];
    

    【讨论】:

    • 请注意,setFinsheSelectedImage:withFinishedUnselectedImage 在 iOS 7 中已弃用。
    • Matthew 如果它在 iOS 7 中被弃用,还有其他方法可以解决这个问题,还是仅仅因为苹果想要保持灰色默认图标标准?
    • @user2792129 - 是的,我花了几分钟才把答案输入到
    • 嘿,如果我不想使用标题..它已经在图像中,我该怎么办。我想使用标题空间来适应标签栏中心的图标。
    • 更新的方式就是[[UITabBar appearance] setTintColor:[UIColor whiteColor]];
    【解决方案2】:

    您需要将每个选项卡的(未选中的)图像的渲染模式设置为UIImageRenderingModeAlwaysOriginal。因此,在您的应用委托中,获取对标签栏的引用,然后遍历每个标签栏项目,调整图像模式。

    可能有更好的方法来获取对标签栏的引用,但我做了以下操作:

    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITabBarController *tbc = [sb instantiateInitialViewController];
    self.window.rootViewController = tbc;
    UITabBar *tb = tbc.tabBar;
    

    那么图像调整可以如下进行:

    NSArray *items = tb.items;
    
    for (UITabBarItem *tbi in items) {
        UIImage *image = tbi.image;
        tbi.selectedImage = image;
        tbi.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    

    【讨论】:

    • 谢谢马修。我将该代码放入我的 .m 应用程序委托中,它返回了一个问题,上面写着“不支持的配置”“导航项 Main.storyboard 中不支持普通样式。”
    • 我的代码假定标签栏控制器是故事板中的初始视图控制器。如果您的应用设置不同,这可能是问题的根源。
    • 这有效,但仅适用于其中一张图像。基本上我有一个未选中和选中的图标。这个技巧使未选中的工作,但选中仍然只是一团糟。
    • @Halsafar 那是因为您只为基础image 设置渲染模式。对selectedImage 执行相同的操作,尽管您可能希望将其设置为不同的图像,否则您无法区分选中和未选中。
    【解决方案3】:

    试试这个方法..它对我有用

    应用内委托

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
    
     UITabBarController *tabBarController=(UITabBarController*)(self.window.rootViewController);
        UITabBar *tabBar=tabBarController.tabBar;
      UITabBarItem *tabBarItem1=[[tabBar items] objectAtIndex:0];//first tab bar
     [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"yourImageSelected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"yourImageUnselected.png"]];//image should be 30 by 30
    }
    

    奔跑吧

    【讨论】:

      【解决方案4】:

      使用选定和未选定的图像设置自定义标签栏。 还有 tabbarItem Image Insets 位置在中心

      UITabBar *tabBar = self.tabBarController.tabBar;
      
      UITabBarItem *item0 = [tabBar.items objectAtIndex:0];
      UITabBarItem *item1 = [tabBar.items objectAtIndex:1];
      UITabBarItem *item2 = [tabBar.items objectAtIndex:2];
      UITabBarItem *item3 = [tabBar.items objectAtIndex:3];
      
      [item0 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue.png"]  withFinishedUnselectedImage:[UIImage imageNamed:@"iconGray.png"] ];
      [item1 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue2.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-2.png"]];
      [item2 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue3.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-3.png"]];
      [item3 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue4.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"icon-4.png"]];
      
      item0.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
      item1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
      item2.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
      item3.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
      

      **在第一个视图控制器的 viewWillAppear 方法中。 **

      【讨论】:

        【解决方案5】:

        因为不推荐使用 setFinishedSelectedImage:withFinishedUnselectedImage,所以我使用了 Ram S 答案的更改版本:

        [item0 setFinishedSelectedImage:[UIImage imageNamed:@"iconBlue.png"]  withFinishedUnselectedImage:[UIImage imageNamed:@"iconGray.png"] ];
        

        与:

        [item0 setImage:[[UIImage imageNamed:@"iconGray.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [item0 setSelectedImage:[[UIImage imageNamed:@"iconBlue.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        

        更多信息请参见UITabBarItem setFinishedSelectedImage: deprecated in iOS7

        【讨论】:

          【解决方案6】:

          如果您在可视化编辑器中有标签栏,则可以在此处进行。选择选项卡栏并在“用户定义的运行时属性”中添加属性: 关键路径:selectedImageTintColor 类型:颜色 值:

          【讨论】:

          • 就像一个魅力。未选中的图标是否也有美丽的魔力?
          【解决方案7】:

          您可以通过添加“用户定义的运行时属性”来纯粹从情节提要中执行此操作,而无需编写任何代码:

          1. 在情节提要中选择您的 UITabViewController
          2. 打开“文档大纲”并确保选择场景中的“标签栏”视图。
          3. 显示“身份检查器”。您应该看到“用户定义的运行时属性”部分
          4. 添加以下内容:
            • 关键路径:tintColor
            • 类型:颜色
            • 值:选择您想要的颜色。

          【讨论】:

          • 很高兴知道。但是用户定义的运行时属性绝对是调试的噩梦。
          • 除了 tintColor 之外,您还能在 Key Path 中输入什么?
          • @RasmusBidstrup,所选IB项目的所有相关属性。在我们的例子中是 UITabBar。所以去苹果文档并阅读 UITabBar 文档。
          • 如果我错了请纠正我,但这会在您选择图像时设置颜色。他想更改项目的默认灰色而不是选定的颜色?