iOS 13`withTintColor`不遵循我指定的颜色 [英] iOS 13 `withTintColor` not obeying the color I assign

查看:275
本文介绍了iOS 13`withTintColor`不遵循我指定的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现新的iOS 13 UIImage属性withTintColor(_:renderingMode:)的行为令人费解.它的作用是什么,它与图像出现的上下文的色调颜色有什么关系?

I find the behavior of the new iOS 13 UIImage property withTintColor(_:renderingMode:) incomprehensible. What is it for, and how does it relate to the tint color of the context in which the image appears?

例如:

let im = UIImage(systemName:"circle.fill")?.withTintColor(.red)
let iv = UIImageView(image:im)
iv.frame.origin = CGPoint(x: 100, y: 400)
self.view.addSubview(iv)

我说了.red.但它是蓝色的:

I said .red. But it's blue:

很明显它是蓝色的,因为它的颜色来自周围环境的色彩.但是,给图像本身着色颜色有什么意义呢?

Evidently it's blue because it takes its color from the tint color of the surrounding context. But then what's the point of giving the image itself a tint color?

但是它变得更加奇怪.我将绘制自己的图像并为其赋予 色彩:

But it gets even weirder. I'll draw my own image and give it a tint color:

let sz = CGSize(width: 20, height: 20)
let im = UIGraphicsImageRenderer(size:sz).image { ctx in
    ctx.cgContext.fillEllipse(in:CGRect(origin:.zero, size:sz))
}.withTintColor(.red)
let iv = UIImageView(image:im)
iv.frame.origin = CGPoint(x: 100, y: 400)
self.view.addSubview(iv)

我说了.red,它是红色的:

导致不一致的原因是什么?

What accounts for the inconsistency?

推荐答案

withTintColor可能主要用于仅与符号图像一起使用,这些符号图像始终被视为模板并且没有其自身的颜色.但是,能够将普通图像作为模板并为其赋予颜色也是很好的.

withTintColor is probably intended primarily for use only with symbol images, which are always treated as templates and have no color of their own. However, it is also very nice to be able to treat an ordinary image as a template and give it a color.

假设您要绘制到自定义UIView的draw(_:)方法中,或在UIImageView图形上下文中进行绘制.模板视图一直存在问题;您无法在此处访问其模板行为,因此无法着色它们. withTintColor解决了这个问题.

Let's say you're drawing into a custom UIView's draw(_:) method, or drawing in a UIImageView graphics context. There has always been a problem with template views; you have no access here to their template behavior, so there is no way to tint them. withTintColor solves that problem.

但是,毫无疑问,当您以任何其他方式使用它们时,这些方法的行为极为奇怪:

There is no doubt, however, that the behavior of these methods is profoundly weird when you use them in any other way:

  • 您说出withTintColor时,即使您说出.alwaysOriginal,生成的图像也被视为模板图像.因此,您的渲染模式现在被忽略了!

  • As soon as you say withTintColor, the resulting image is treated as a template image, even if you say .alwaysOriginal. So your rendering mode is now being ignored!

此外,在已经有tintColor的上下文中,现在可以有两种相互竞争的着色颜色:上下文的颜色(周围的视图或继承的tintColor)和当您说withTintColor时的图像.如果这是 template 图像(符号图像或.alwaysTemplate图像,或模板上下文中的图像,如按钮),则 context的 tintColor获胜,您说出withTintColor专门应用的颜色将被忽略!

Moreover, in contexts where there is already a tintColor, there can now be two competing tint colors: that of the context (the surrounding view or the inherited tintColor) and that of the image when you say withTintColor. And if this is a template image — a symbol image, or an .alwaysTemplate image, or an image in a template context like a button — then the context's tintColor wins, and the color that you specifically applied by saying withTintColor is ignored!!

例如,即使您将符号图像的色调颜色专门设置为红色,这也会导致蓝色符号图像(因为默认的图像视图tintColor是蓝色):

For example, this results in a blue symbol image (because the default image view tintColor is blue) even though you specifically set the symbol image's tint color to red:

let im = UIImage(systemName:"circle.fill")?.withTintColor(.red)
let iv = UIImageView(image:im)

要获得红色的符号图像,您必须这样说:

To get a red symbol image, you have to say this:

let im = UIImage(systemName:"circle.fill")?.withTintColor(.red,
    renderingMode: .alwaysOriginal)

因此,在第一个代码中,您分配的色彩被忽略(我们变成蓝色,而不是红色),但是在第二个代码中,您分配的渲染模式被忽略(它仍然是模板图像,但现在是根据要求红色).奇怪吗?

So in the first code, the tint color you assign is being ignored (we get blue, not red), but in the second code, the rendering mode you assign is being ignored (it's still a template image, but now it's red as requested). Weird, eh?

(我很难相信这是预期的行为,并且我已经提交了一个错误,但到目前为止苹果没有任何回应.)

(I find it very hard to believe that this is the intended behavior, and I've filed a bug, but so far no response from Apple.)

这篇关于iOS 13`withTintColor`不遵循我指定的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆