ios UIImageView 并不总是为模板图像着色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41121425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIImageView doesn't always tint template image
提问by Zuzana Paulis
In the case below, there are two UIImageViews with the same settings and the same template image... But one tints the image, and one does not
在下面的例子中,有两个 UIImageViews 具有相同的设置和相同的模板图像......但是一个给图像着色,一个没有
I duplicated working UIImageView
and placed it instead of the other and it worked. This happened to me multiple times and this solution always worked, but I still wonder what could I have done wrong? Can it be an Xcode bug? Did something similar happen to you? I have Xcode 8.1.
我复制了工作UIImageView
并将其放置在另一个位置,然后它就起作用了。这发生在我身上多次,这个解决方案总是有效,但我仍然想知道我做错了什么?会不会是 Xcode 的错误?类似的事情发生在你身上吗?我有 Xcode 8.1。
回答by David Rees
Easy fix solution:
简单修复解决方案:
Just add a new runtime attribute which will set the tintColor of the UIImageView to the specified color and ensure the image is tinted.
只需添加一个新的运行时属性,它将 UIImageView 的 tintColor 设置为指定的颜色并确保图像着色。
You will still need to set your image to be rendered as a template image in your Images.xcassets file.
您仍然需要在 Images.xcassets 文件中将图像设置为呈现为模板图像。
This way you dont need any additional outlets, extensions or lines of code.
这样你就不需要任何额外的插座、扩展或代码行。
Also take note:It will not apply the tintColor in the user defined attribute if the tintColor on the view is the same color, they must be different.
还要注意:如果视图上的 tintColor 是相同的颜色,则不会应用用户定义属性中的 tintColor,它们必须不同。
回答by allaire
Best solution I found that doesn't require a subclass or another IBInspectable
attribute:
我发现不需要子类或其他IBInspectable
属性的最佳解决方案:
import UIKit
extension UIImageView {
override open func awakeFromNib() {
super.awakeFromNib()
tintColorDidChange()
}
}
回答by richardjsimkins
The problem stems from the value for tint having no null state (within the Storyboard/Interface Builder UI). In order for Xcode to decide whether a tint should be applied to a UIImageView
there appears to be an additional test. In Xcode this is apparently done by evaluating the constraints applied to UIImageView
. As far as I know this test is undocumented.
问题源于 tint 的值没有空状态(在 Storyboard/Interface Builder UI 中)。为了让 Xcode 决定是否应将色调应用于UIImageView
似乎有一个额外的测试。在 Xcode 中,这显然是通过评估应用于UIImageView
. 据我所知,这个测试没有记录。
If you only have a "Spacing to nearest neighbor" constraint on two (or one) sides of your UIImageView
then the tint is not applied regardless of the value set for UIImage.renderingMode
.
如果您的两侧(或一侧)只有“与最近邻的间距”约束,UIImageView
则无论为 设置的值如何,都不会应用色调UIImage.renderingMode
。
If you have a "Spacing to nearest neighbor" constraint on three (or all) sides of your UIImageView
then the tint is applied if the UIImage.renderingMode
is set to .alwaysTemplate
.
如果在您的三个(或所有)边上有“与最近邻的间距”约束,UIImageView
那么如果UIImage.renderingMode
设置为 ,则应用色调.alwaysTemplate
。
In a purely Storyboard/Interface Builder approach you set the UIImage.renderingMode
of an image by adding it to an Asset Catalogue and then changing the "Render As" property of the Image Set to "Template Image".
在纯粹的 Storyboard/Interface Builder 方法中,您可以UIImage.renderingMode
通过将图像添加到资产目录,然后将图像集的“渲染为”属性更改为“模板图像”来设置图像的 。
回答by Paramasivan Samuttiram
Try setting tintColor
programmatically; it fixed the issue for me.
尝试以tintColor
编程方式设置;它为我解决了这个问题。
回答by paddlefish
I think there is a bug in UIImageView. If you set a tint color in the storyboard, it doesn't apply that tint even when the asset is set up to render as template.
我认为 UIImageView 中有一个错误。如果您在情节提要中设置色调颜色,即使将资产设置为呈现为模板,它也不会应用该色调。
The workaround is to apply the tint color to the UIImageView's parent view in the storyboard.
解决方法是将色调颜色应用于故事板中 UIImageView 的父视图。
Or, set the tintColor to nil and then back again in code by binding the UIImageView to an outlet.
或者,将 tintColor 设置为 nil,然后通过将 UIImageView 绑定到一个插座再次在代码中返回。
回答by Mina
use this extension to set UIImageView tintColor from interface builder.
使用此扩展从界面构建器设置 UIImageView tintColor。
extension UIImageView {
@IBInspectable var imageColor: UIColor! {
set {
super.tintColor = newValue
}
get {
return super.tintColor
}
}
}
回答by Marlo Quidato
Runtime attribute trick did not work for me. I made this class just for fixing this kind of stuff. You'll just set to this class all the problematic image views.
运行时属性技巧对我不起作用。我制作这个类只是为了修复这种东西。您只需将所有有问题的图像视图设置为此类。
final class TintedImageView: UIImageView {
// WORKAROUND: Template images are not tinted when set using the Interface Builder.
override func layoutSubviews() {
super.layoutSubviews()
let preferredTintColor = tintColor
tintColor = nil
tintColor = preferredTintColor
}
}
回答by rudymatos
By removing my existing UIImageView and adding a new one my problem was gone. Apparently, the situation is that you should add the UIImageView AFTER adding the images to the Assets folder.
通过删除我现有的 UIImageView 并添加一个新的,我的问题就消失了。显然,情况是您应该在将图像添加到 Assets 文件夹后添加 UIImageView。
回答by Reimond Hill
Maybe it helps as I was banging my head for an hour until I realised something that I still quite don't understand.
也许这会有所帮助,因为我敲了一个小时的头,直到我意识到我仍然不太明白的事情。
I had my view in my .nib file and all the outlets except one (the UIImageView) had a non default tint colour. I tried all the suggested answer but the issue was solved by selecting a default tint colour for changing it after programatically.
我在我的 .nib 文件中有我的视图,除了一个(UIImageView)之外的所有插座都有一个非默认的色调颜色。我尝试了所有建议的答案,但问题是通过选择默认色调颜色以在编程后更改它来解决的。
Might it a bug?
可能是一个错误?