使自定义图像尊重tintColor属性iOS [英] Make custom image respect tintColor property iOS

查看:90
本文介绍了使自定义图像尊重tintColor属性iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须想象已经多次询问过这个问题,但我不能正确地写出我的问题。

I have to imagine this has been asked several times, but I must not be wording my question correctly.

我有自己的自定义图像我是在photoshop中制作的它被设置为按钮的图像属性。这里显示正常:

I have my own custom image I made in photoshop and it is set as a button's image property. Here it is showing normally:

背景是透明的,但它是44x44,三个点是一个88x88像素的.png文件。所以如果我做这样的事情:

The background is transparent, but it is 44x44 with the three dots being a 88x88 pixel .png file. So if I do something like this:

self.theButton.tintColor = [UIColor redColor];

图像仍然显示为白色。我不是交易设计师,所以我似乎不知道如何正确保存或设置文件以使它们尊重tintColor属性。在photoshop中,我通常使用保存为网络保存这些内容。

the image still shows as white. I am not a designer by trade, so I seem to be missing how to properly save or set up the files to make them respect the tintColor property. In photoshop, I usually save these using "Save for web".

谢谢

推荐答案

您正在寻找的是模板图像。

What you are looking for are stencil images.

来自UIKit用户界面目录:

From the UIKit User Interface Catalog:


模板图片

Template Images

在iOS 7中,您可以选择将应用中的任何图片视为
模板 - 或模板图像。当您选择将图像视为
模板时,系统会忽略图像的颜色信息,并根据图像中的alpha值创建
图像模板(
图像的一部分带有小于1.0的alpha值被视为完全
透明)。然后可以使用tintColor将此模板重新着色为
,与您的用户界面的其余部分匹配。

In iOS 7, you can choose to treat any of the images in your app as a template—or stencil—image. When you elect to treat an image as a template, the system ignores the image’s color information and creates an image stencil based on the alpha values in the image (parts of the image with an alpha value of less than 1.0 get treated as completely transparent). This stencil can then be recolored using tintColor to match the rest of your user interface.

默认情况下,使用$ b $创建图像(UIImage) b UIImageRenderingModeAutomatic。如果您在图像上设置了
UIImageRenderingModeAutomatic,则会根据其上下文将其视为
模板或原始模板。某些UIKit
元素 - 包括导航栏,标签栏,工具栏,分段
控件 - 自动将其前景图像视为模板,
虽然它们的背景图像被视为原始图像。其他
元素 - 例如图片视图和网络视图 - 将其图像视为
原件。如果您希望始终将您的图像视为模板
而不考虑上下文,请设置UIImageRenderingModeAlwaysTemplate;如果您
希望您的图像始终被视为原始图像,请设置
UIImageRenderingModeAlwaysOriginal。

By default, an image (UIImage) is created with UIImageRenderingModeAutomatic. If you have UIImageRenderingModeAutomatic set on your image, it will be treated as template or original based on its context. Certain UIKit elements—including navigation bars, tab bars, toolbars, segmented controls—automatically treat their foreground images as templates, although their background images are treated as original. Other elements—such as image views and web views—treat their images as originals. If you want your image to always be treated as a template regardless of context, set UIImageRenderingModeAlwaysTemplate; if you want your image to always be treated as original, set UIImageRenderingModeAlwaysOriginal.

要指定图像的渲染模式,请先创建一个标准的
图像,然后在该图像上调用imageWithRenderingMode:方法。

To specify the rendering mode of an image, first create a standard image, and then call the imageWithRenderingMode: method on that image.

UIImage * myImage = [UIImage imageNamed:@myImageFile.png]; myImage =
[myImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

UIImage *myImage = [UIImage imageNamed:@"myImageFile.png"]; myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

这篇关于使自定义图像尊重tintColor属性iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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