我有一个名为theImageView的UIImageView, UIImage是单色的(透明背景),就像下面左边的黑色心形。我如何在iOS 7或更高的系统中,根据iOS 7+导航栏图标中使用的色调方法,以编程方式更改此图像的色调颜色?
这个方法也适用于WatchKit的Apple Watch应用程序吗?
我有一个名为theImageView的UIImageView, UIImage是单色的(透明背景),就像下面左边的黑色心形。我如何在iOS 7或更高的系统中,根据iOS 7+导航栏图标中使用的色调方法,以编程方式更改此图像的色调颜色?
这个方法也适用于WatchKit的Apple Watch应用程序吗?
当前回答
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中,您可以为模板图像设置色调颜色。
您必须将图像添加到WatchKit应用程序中的资产目录,并将图像集设置为在属性检查器中呈现为模板图像。与iPhone应用程序不同,您目前不能在WatchKit扩展的代码中设置模板渲染。 设置该图像用于你的WKInterfaceImage在界面生成器为你的应用程序 创建一个IBOutlet在你的WKInterfaceController为WKInterfaceImage称为“theImage”…
然后在Swift 3或4中设置色调颜色:
theImage.setTintColor(UIColor.red)
斯威夫特2:
theImage.setTintColor(UIColor.redColor())
然后在Objective-C中设置色调颜色:
[self.theImage setTintColor:[UIColor redColor]];
如果您使用模板图像并且没有应用着色颜色,则将应用WatchKit应用程序的全局着色。如果你没有设置全局色调,当作为模板图像使用时,图像将默认为浅蓝色。
其他回答
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中,您可以为模板图像设置色调颜色。
您必须将图像添加到WatchKit应用程序中的资产目录,并将图像集设置为在属性检查器中呈现为模板图像。与iPhone应用程序不同,您目前不能在WatchKit扩展的代码中设置模板渲染。 设置该图像用于你的WKInterfaceImage在界面生成器为你的应用程序 创建一个IBOutlet在你的WKInterfaceController为WKInterfaceImage称为“theImage”…
然后在Swift 3或4中设置色调颜色:
theImage.setTintColor(UIColor.red)
斯威夫特2:
theImage.setTintColor(UIColor.redColor())
然后在Objective-C中设置色调颜色:
[self.theImage setTintColor:[UIColor redColor]];
如果您使用模板图像并且没有应用着色颜色,则将应用WatchKit应用程序的全局着色。如果你没有设置全局色调,当作为模板图像使用时,图像将默认为浅蓝色。
自iOS 13以来,UIImage有一个原生方法
let image = yourImage.withTintColor(.systemRed)
现在我使用基于Duncan Babbage response的方法:
+ (UIImageView *) tintImageView: (UIImageView *)imageView withColor: (UIColor*) color{
imageView.image = [imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[imageView setTintColor:color];
return imageView;
}
另外,对于以上的答案,在iOS 13及以后有一个干净的方法
let image = UIImage(named: "imageName")?.withTintColor(.white, renderingMode: .alwaysTemplate)
如果有人关心没有UIImageView的解决方案:
// (Swift 3)
extension UIImage {
func tint(with color: UIColor) -> UIImage {
var image = withRenderingMode(.alwaysTemplate)
UIGraphicsBeginImageContextWithOptions(size, false, scale)
color.set()
image.draw(in: CGRect(origin: .zero, size: size))
image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}