2013-01-14 45 views
0

我试图使用UIButton图像视图的背景视图,但由于某种原因它不会显示出来。这是我曾尝试:按钮中的图像视图不会出现

detailCell.greenPriorityButton.imageView.frame = detailCell.greenPriorityButton.frame; 
[detailCell.greenPriorityButton.imageView setBackgroundColor:[UIColor greenColor]]; 
[detailCell.greenPriorityButton.imageView setHidden:NO]; 
[detailCell.greenPriorityButton.imageView setOpaque:YES]; 

我已经呼吁imageView财产NSLog,似乎一切都是理所应当的。我也可以点击按钮和与之相关的方法将被调用,所以我知道它存在。以防万一,我在这里失去了一些东西是NSLog回报:

<UIImageView: 0x1d97e1b0; frame = (254 61; 20 20); clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x1d97e210>> 

我不使用点标记来设置上面的图片查看其属性的原因是因为他们似乎没有改变的值。例如,如果我说detailCell.greenPriorityButton.imageView.hidden = NO;它似乎没有做任何事情。

感谢您的帮助!

编辑

其原因不只是设置按钮的背景颜色,而不是自己的形象的观点是,因为我想创建的按钮小的形状。不过,我希望可定位空间在形状周围留有边距,因此它仍然是用户友好的。我认为图像视图属性可以提供有用的信息,因为我可以根据框架和按钮层分别操作框架和图层。

我已经尝试添加一个UIView *greenBackground作为按钮的子视图,但是这也不会出现。

回答

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]; 
+0

我实际上并不想给它一个图像。我只想使用'imageView'属性的'backgroundColor'属性。 –

+0

该按钮是一个自定义类型按钮吗? – rooster117

+0

如果这是你的意思,我没有子类'UIButton'。我通过故事板将该按钮添加到视图中,并通过“IBOutlet”连接。然而,它所在的视图是一个“UITableViewCell”子类。 –