我正在尝试使用UIButton
的图片视图的背景视图,但由于某种原因它不会显示。这就是我的尝试:
detailCell.greenPriorityButton.imageView.frame = detailCell.greenPriorityButton.frame;
[detailCell.greenPriorityButton.imageView setBackgroundColor:[UIColor greenColor]];
[detailCell.greenPriorityButton.imageView setHidden:NO];
[detailCell.greenPriorityButton.imageView setOpaque:YES];
我在NSLog
属性上调用了imageView
,看起来一切都是应该的。我也可以点击按钮,将调用与之关联的方法,因此我知道它存在。以防我在这里遗漏的是NSLog
返回:
<UIImageView: 0x1d97e1b0; frame = (254 61; 20 20); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x1d97e210>>
我不使用点表示法来设置上面的图像视图属性的原因是因为它们似乎没有更改值。例如,如果我说detailCell.greenPriorityButton.imageView.hidden = NO;
它似乎没有做任何事情。
感谢您的帮助!
修改
不仅设置按钮的背景颜色而不是图像视图的原因是因为我试图在按钮中创建一个小形状。但是我希望tappable空间在形状周围有边距,所以它仍然是用户友好的。我认为图像视图属性会有用,因为我可以分别从按钮的框架和图层操作框架和图层。
我现在尝试将UIView *greenBackground
添加为按钮的子视图,但这也不会出现。
答案 0 :(得分:1)
如果你想要一个可以在按钮视图中显示的视图以便设置其颜色,那么我认为尝试使用imageview是错误的方法。您只需添加具有更改背景颜色的子视图即可。像这样的东西:
UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 10, 10)];//Whatever rect you want that will be in reference to the frame of the button
colorView.backgroundColor = [UIColor greenColor];
[detailCell.greenPriorityButton addSubview:colorView];