我有自己的 UIButton
子类.我在其上添加 UIImageView
并添加图像.我想用浅色在图像上绘制它,但它不起作用.
I have my own subclass of UIButton
. I add UIImageView
on it and add an image. I would like to paint it over the image with a tint color but it doesn't work.
到目前为止我有:
- (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];
...
并在选择时:
- (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.)
代替这段代码:
[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
你应该有:
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
在 Swift 4.1
image = UIImage(named: "name")!.withRenderingMode(.alwaysTemplate)
这篇关于在 UIImageView 上使用 Tint 颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!