【问题标题】:To change the color of unselected UITabBar icon in iOS 7?更改 iOS 7 中未选中的 UITabBar 图标的颜色?
【发布时间】:2014-03-03 00:30:34
【问题描述】:

我知道之前也有人问过这个问题,但是我在互联网上搜索解决方案仍然没有得到任何解决方案。

我参考了以下帖子:

How can I change the text and icon colors for tabBarItems in iOS 7? 只能使用tintColor 更改所选图标的颜色。

How to change the color of unselected tab bar items in iOS 7? 在此他们编写了自己的 GozTabBar 类,继承自 UIView

我想更改UITabBar图标在未选中状态时的默认灰色。

任何帮助将不胜感激。提前致谢。

【问题讨论】:

标签: ios ios7 uitabbarcontroller uitabbar uitabbaritem


【解决方案1】:

如果您已经使用 Storyboard 配置了标签栏图像,只需在您的第一个视图的 ViewDidLoad 中调用此方法:

-(void) configTabBar
{     
    UITabBarController *tabBarController = [self tabBarController];
    UITabBar *tabBar = tabBarController.tabBar;

    for (UITabBarItem  *tab in tabBar.items) {
        tab.image = [tab.image imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
        tab.selectedImage = [tab.image imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
    }
}

【讨论】:

  • SWIFT:tab.image.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
【解决方案2】:

将 UIControlStateHighlighted 更改为 iOS8 的 UIControlStateSelected

 [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor whiteColor], NSForegroundColorAttributeName,
                                                           nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           titleHighlightedColor, NSForegroundColorAttributeName,
                                                           nil] forState:UIControlStateSelected]

【讨论】:

  • 这只会改变标题文本的颜色。不是图标本身的颜色。
【解决方案3】:

我假设您不想使用 tintColor 更改颜色?另一种选择是使用两个看起来完全相同但颜色不同的图像。一张图片是选中的标签,另一张是未选中的。

在你的 AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 函数中,试试这个。

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;

// repeat for every tab, but increment the index each time
UITabBarItem *firstTab = [tabBar.items objectAtIndex:0];

// also repeat for every tab
firstTab.image = [[UIImage imageNamed:@"someImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
firstTab.selectedImage = [[UIImage imageNamed:@"someImageSelected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

编辑:对于那些没有标签栏控制器作为根视图控制器的人,您可以像这样抓住控制器,其余代码相同。

UITabBarController *tabBarController = self.tabBarController;

【讨论】:

  • 如果您的 UITabBarController 是您的应用程序的 rootviewcontroller,它可以工作,但它不适用于您添加到 UINavigationController 的 UITabBarController(或者我做错了什么)。我会继续搜索
  • @phyzalis 它应该可以工作。你必须往上走。获取对您的导航控制器(根)的引用,然后您的标签栏应该是该控制器的根。
  • @cocoanut 你是对的,我很抱歉我从来没有评论我是如何做到的。事实上,我必须在 UITabBarController 实例化时直接执行此操作(在我的情况下,就在 [storyboard instantiateViewControllerWithIdentifier:...] 之后)。感谢您的评论
  • @cocoanutmobile 你能解释一下我是如何进入层次结构的,接下来该怎么做?谢谢
  • 在 iOS 9.1 中也为我工作
【解决方案4】:
[[UITabBar appearance] setTintColor:[UIColor colorWithRed:252/255.0 green:218/255.0 blue:49/255.0 alpha:1.0]];

tabBarItem1.image = [[UIImage imageNamed:@"home_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabBarItem1.selectedImage = [UIImage imageNamed:@"home_icon_selected.png"];

[[UITabBar appearance] setBackgroundColor:[UIColor colorWithRed:15/255.0 green:85/255.0 blue:160/255.0 alpha:1.0]];
    // Change the title color of tab bar items
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor whiteColor], NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateNormal];
    UIColor *titleHighlightedColor = [UIColor colorWithRed:252/255.0 green:218/255.0 blue:49/255.0 alpha:1.0];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       titleHighlightedColor, NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateHighlighted]

【讨论】: