[英]Using Tint color on UIImageView
I have my own subclass of UIButton
.我有自己的
UIButton
子类。 I add UIImageView
on it and add an image.我在其上添加
UIImageView
并添加图像。 I would like to paint it over the image with a tint color but it doesn't work.我想用淡色将它涂在图像上,但它不起作用。
So far I have:到目前为止,我有:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.clipsToBounds = YES;
self.circleView = [[UIView alloc]init];
self.circleView.backgroundColor = [UIColor whiteColor];
self.circleView.layer.borderColor = [[Color getGraySeparatorColor]CGColor];
self.circleView.layer.borderWidth = 1;
self.circleView.userInteractionEnabled = NO;
self.circleView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview:self.circleView];
self.iconView = [[UIImageView alloc]init];
[self.iconView setContentMode:UIViewContentModeScaleAspectFit];
UIImage * image = [UIImage imageNamed:@"more"];
[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
self.iconView.image = image;
self.iconView.translatesAutoresizingMaskIntoConstraints = NO;
[self.circleView addSubview:self.iconView];
...
and on selection :和选择:
- (void) setSelected:(BOOL)selected
{
if (selected) {
[self.iconView setTintColor:[UIColor redColor]];
[self.circleView setTintColor:[UIColor redColor]];
}
else{
[self.iconView setTintColor:[UIColor blueColor]];
[self.circleView setTintColor:[UIColor blueColor]];
}
}
What did I do wrong?我做错了什么? (The color of the image always stays the same as it was originally.)
(图像的颜色始终保持原来的颜色。)
Instead of this code:而不是这个代码:
[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
you should have:你应该有:
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
Use this in Swift 4.1在Swift 4.1 中使用它
image = UIImage(named: "name")!.withRenderingMode(.alwaysTemplate)
(Can't edit @Zhaolong Zhong post) (无法编辑@Zhaolong Zhong 帖子)
In swift 3.0 , you can do:在 swift 3.0 中,您可以执行以下操作:
let image = UIImage(named: "your_image_name")!.withRenderingMode(.alwaysTemplate)
yourImageView.image = image
yourImageView.tintColor = UIColor.blue
In swift 2.0+, you can do:在 swift 2.0+ 中,您可以执行以下操作:
let image = UIImage(named: "your_image_name")!.imageWithRenderingMode(.AlwaysTemplate)
yourImageView.image = image
yourImageView.tintColor = UIColor.blueColor()
Swift version: 5.2斯威夫特版本:5.2
let tintableImage = UIImage(named: "myImage")?.withRenderingMode(.alwaysTemplate)
imageView.image = tintableImage
imageView.tintColor = UIColor.red
Objective C目标 C
self.imgView.image = [self.imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[self.imgView setTintColor:[UIColor darkGrayColor]];
Or或者
You can also just set this on your asset.您也可以在您的资产上设置它。
One step further.更进一步。 This is a drop-in subclass of UIImageView.
这是 UIImageView 的一个嵌入式子类。 (Not exact solution for original question.) Use in Interface Builder by setting class name to TintedImageView.
(不是原始问题的确切解决方案。)通过将类名设置为 TintedImageView 在 Interface Builder 中使用。 Updates in real-time inside the designer as tint color changes.
随着色调颜色的变化,在设计器内部实时更新。
(Swift 3.1, Xcode 8.3) (Swift 3.1,Xcode 8.3)
import UIKit
@IBDesignable class TintedImageView: UIImageView {
override func prepareForInterfaceBuilder() {
self.configure()
}
override func awakeFromNib() {
super.awakeFromNib()
self.configure()
}
@IBInspectable override var tintColor: UIColor! {
didSet {
self.configure()
}
}
private func configure() {
self.image = self.image?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
}
}
Make the imageView制作图像视图
let imageView = UIImageView(frame: frame!)
imageView.contentMode = .ScaleAspectFit
imageView.tintColor = tintColor
Make the image制作图像
let mainBundle = NSBundle.mainBundle()
var image = UIImage(named: filename!, inBundle: mainBundle, compatibleWithTraitCollection: nil)
image = image?.imageWithRenderingMode(.AlwaysTemplate)
Wire them together将它们连接在一起
imageView?.image = image
Display it显示它
view.addSubview(imageView)
all said is correct.说的都是对的。 my contribution If You cannot / dont want to apply to every UiImageView, OR for efficiency You need to render ONCE (or example for cells of tableviews)
我的贡献如果你不能/不想应用到每个 UiImageView,或者为了效率你需要渲染一次(或者例如 tableviews 的单元格)
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
}
And set to all UI elements this UIImage.并将此 UIImage 设置为所有 UI 元素。
@odemolliens answer should just work. @odemolliens 的答案应该可以正常工作。
But if you are still having issues, make sure that the tint color you are applying to the UIImageView is different from the one defined in the Interface Builder.但是,如果您仍然遇到问题,请确保您应用到 UIImageView 的色调颜色与 Interface Builder 中定义的颜色不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.