xcode 7.3 自定义图像在故事板中显示为灰色

xcode 7.3 custom image is grayed out in storyboard

我在 xcode 项目中将自定义图像添加到 assets.xcassets 作为 3x。它显示正常。 接下来,我通过选择系统项目作为自定义并将所选图像作为自定义图像将图像添加到选项卡栏项目。还通过选择自定义图像填充栏项目图像。

在我的故事板中,图像图标在选项卡栏项目和栏项目中完全变灰。

图像是在 Gimp 中创建的,透明图像大小缩放为 75x75

谁能告诉我如何解决这个问题?

在 iOS7 之后,您可以设置 UIImage 是否使用当前视图的色调渲染 imageWithRenderingMode:

始终原创

Always draw the original image, without treating it as a template.

AlwaysTemplate

Always draw the image as a template image, ignoring its color information.

自动

Use the default rendering mode for the context where the image is used.

所以你想要的是:

self.barItem.image = UIImage(named: "yourImage")?.imageWithRenderingMode(.AlwaysOriginal)

通过将 renderingMode 设置为 AlwaysOriginal,UIImage 将始终绘制原始图像,不会应用模板。然后你就可以得到你想要的。

祝你好运:-)

如@luiyezheng所说,这是图像渲染模式造成的。 将呈现模式应用于 TabBar 中所有项目的更好方法是将此代码放入 TabBarController viewDidLoad 方法中:

 for item in self.tabBar.items! {
            item.image = item.image?.imageWithRenderingMode(.AlwaysOriginal)
            item.selectedImage = item.selectedImage?.imageWithRenderingMode(.AlwaysOriginal)
        }