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

标签 ios objective-c tintcolor

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

我有自己在 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/

相关文章:

IOS/objective-C : Subview of Subview not displaying

ios - 导航栏色调颜色变化

ios - iOS 中的 iswalpha() 在 iOS 上返回的值与在 MacOS 上返回的值不同

ios - OutputStream hasSpaceAvailable 永远不会变成 'true'

iphone - 如何将 iPod 库中的歌曲复制到应用程序并使用 AVAudioPlayer 播放?

iphone - CCTimer 没有触发它应该触发的方法?也 selfSchedule 覆盖自己

ios - SpriteKit - 如何处理多种屏幕尺寸?

ios - Google Analytics for iOS 不发送事件

ios - UITabBarItem selectedImageTintColor。如何找到要设置为 tintcolor 的颜色

swift - UIActivityViewController:访问导航项以更改呈现 View 的颜色