2013-01-14 47 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」子類。 –