gpt4 book ai didi

ios - 使自定义图像尊重 tintColor 属性 iOS

转载 作者:可可西里 更新时间:2023-11-01 03:23:46 24 4
gpt4 key购买 nike

我不得不想象这个问题已经被问过好几次了,但我的问题措辞肯定不正确。

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

enter image description here

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

self.theButton.tintColor = [UIColor redColor];

图像仍然显示为白色。我不是贸易设计师,所以我似乎缺少如何正确保存或设置文件以使它们尊重 tintColor 属性。在 photoshop 中,我通常使用“保存为 web 格式”保存这些。

谢谢

最佳答案

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

来自 UIKit 用户界面目录:

Template Images

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.

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.

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];

关于ios - 使自定义图像尊重 tintColor 属性 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20891491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com