【发布时间】:2014-04-05 21:55:17
【问题描述】:
我有自己的UIButton 子类。我在上面添加UIImageView 并添加图像。我想用浅色在图像上绘制它,但它不起作用。
到目前为止我有:
- (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]];
}
}
我做错了什么? (图像的颜色始终保持原来的颜色。)
【问题讨论】:
-
您在创建 iconView 时可以
setTintColor吗? -
你的意思是在self.iconView = [UIImageView alloc]之后...?是的,我可以,但它不起作用。
-
然后使用 CGContext。也许你可以在这里找到你的答案stackoverflow.com/a/19275079/1790571
-
是的,我看到了这篇文章,但我真的不明白为什么我的代码不起作用。使用 tint color 是更干净的路径。
标签: ios xcode uiimageview uibutton tintcolor