保留所选和未选定UITabBar图标的原始图像颜色 [英] Preserving the original image color of the selected and unselected UITabBar icons

查看:76
本文介绍了保留所选和未选定UITabBar图标的原始图像颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结构如下:

在我的故事板中,我有一个标签栏控制器其中包含一个标签栏对象。这个对象有一个自定义类,我只有这个方法:

In my storyboard, I have a Tab Bar Controller which contains a Tab Bar object. This object has a custom class in which I have only this method:

- (void)awakeFromNib
{
  NSArray *imageNames = @[@"test1", @"test2", @"test3", @"test4", @"test5"];
    for (int i=0; i<5; i++) {
      UITabBarItem *item = [self.items objectAtIndex:i];
      NSString *imageName = [imageNames objectAtIndex:i];
      item.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
      item.selectedImage = [UIImage imageNamed:[imageName stringByAppendingString:@"-selected"]];
    }
}

我创建了资产目录,其中包含我的标签栏图标,包括选定和未选择的版本(不同的颜色)。

I have created an Asset Catalog which contains my tab bar icons, both the selected and unselected versions (different colors).

UIImageRenderingModeAlwaysOriginal 提及提示始终绘制原始图像,而不将其视为模板。应保留原始图像颜色。这似乎没有发生,建议不起作用无论是。

The documentation for UIImageRenderingModeAlwaysOriginal mentions Always draw the original image, without treating it as a template. which should preserve the original image color. This doesn't seem to happen and this suggestion didn't work either.

相反,在选定状态下,我的标签栏图标会获得默认的蓝色色调。

我注意到的一件事是,如果我执行以下操作(取自这里 didFinishLaunchingWithOptions AppDelegate 中,然后我可以设置颜色我想要的所选标签栏图标:

One thing that I noticed is that if I do the following (taken from here) in the didFinishLaunchingWithOptions in the AppDelegate, then I can set the color of the selected tab bar icons as I want:

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

setTintColor 的文档提及:


要应用于标签栏标签栏项目的色调颜色。
从iOS 7.0开始,适用于标签栏标签栏项目的色调颜色是视图层次结构中的第一个非默认色调颜色,从标签栏本身开始。

The tint color to apply to the tab bar’s tab bar items. Starting in iOS 7.0, the tint color that applies to a tab bar’s tab bar items is the first nondefault tint color in the view hierarchy, starting with the tab bar itself.

这是否意味着无论我的标签栏图标的颜色(图像本身的原始图像),在选定状态下,它们总是具有 tintColor <的颜色/ code>?

Does this mean that regardless of my tab bar icons's color (original in the images themselves), in the selected state they will always have the color of tintColor ?

或者我做错了什么/遗失了什么?

Or am I doing something wrong/missing something?

我在iOS 7.0上遇到过这个问题7.1。

I'm having this issue on iOS 7.0 and 7.1.

推荐答案

完美的问题,非常好解释。

Perfect question, really well explained.

你没有为所选图像设置 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal

You are not setting imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal for the selected image.

应添加 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal selectedImage

  item.selectedImage = [[UIImage imageNamed:[imageName stringByAppendingString:@"-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

检查另一个回答

这篇关于保留所选和未选定UITabBar图标的原始图像颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆