OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 iOS 和 WatchKit 中更改图像 tintColor [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:55
标题: ios - 如何在 iOS 和 WatchKit 中更改图像 tintColor

我有一个名为“theImageView”的 UIImageView,UIImage 是单一颜色的(透明背景),就像下面的左黑心一样。如何根据 iOS 7+ 导航栏图标中使用的 tint 方法在 iOS 7 或更高版本中以编程方式更改此图像的色调颜色?

此方法是否也适用于 Apple Watch 应用的 WatchKit?

enter image description here



Best Answer-推荐答案


iOS
对于 iOS 应用,在 Swift 3、4 或 5 中:

theImageView.image = theImageView.image?.withRenderingMode(.alwaysTemplate)
theImageView.tintColor = UIColor.red

对于 Swift 2:

theImageView.image = theImageView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
theImageView.tintColor = UIColor.redColor()

同时,现代的 Objective-C 解决方案是:

theImageView.image = [theImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[theImageView setTintColor:[UIColor redColor]];

Watchkit
在 Apple Watch 应用程序的 WatchKit 中,您可以设置 tint color for a template image .

  1. 您必须将图像添加到 WatchKit 应用中的 Assets 目录中,并在属性检查器中将图像集设置为呈现为模板图像。与 iPhone 应用不同的是,目前 WatchKit Extension 中无法在代码中设置模板渲染。
  2. 在应用的界面构建器中设置要在 WKInterfaceImage 中使用的图像
  3. 在 WKInterfaceController 中为名为“theImage”的 WKInterfaceImage 创建一个 IBOutlet...

然后在 Swift 3 或 4 中设置色调颜色:

theImage.setTintColor(UIColor.red)

swift 2:

theImage.setTintColor(UIColor.redColor())

然后在 Objective-C 中设置 tint 颜色:

[self.theImage setTintColor:[UIColor redColor]];

如果您使用模板图像并且未应用色调颜色,则会应用您的 WatchKit 应用的全局色调。如果没有设置全局色调,theImage 用作模板图像时默认为浅蓝色。

关于ios - 如何在 iOS 和 WatchKit 中更改图像 tintColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40202163/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4