2014-10-05 37 views
1

我已將UIButton的正常狀態設置爲一個圖像,並將其突出顯示的狀態設置爲另一個圖像。按鈕正常沒問題,但當我按下按鈕時,它會將其設置爲應該是的圖像的灰色漸變版本。在下面的圖片中,頂部灰色按鈕被按下,其他按鈕處於正常狀態。這些按鈕以編程方式製作。當按下UIButton時會褪色

UIButton *button; 

UIImage *bluenormalImage = [UIImage imageNamed:@"bluebuttonnormal.png"]; 
UIImage *bluepressedImage = [UIImage imageNamed:@"bluebuttonpushed.png"]; 

[button setBackgroundImage:bluenormalImage forState:UIControlStateNormal]; 
[button setBackgroundImage:bluepressedImage forState:UIControlStateHighlighted]; 

top button is clicked

+0

是不是按鈕爲空指針? – Rambatino 2014-10-06 01:25:06

回答

2

OK,我不知道該文本不是圖像的一部分。做這個,而不是:

從我能看到你需要正確初始化按鈕作爲自定義類型。突出問題發生在按鈕類型是系統時。

您的按鈕變線更改爲以下:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 

如果以後初始化它,你可以設置背景圖片之前,請執行下列操作。

UIButton *button 
button = [UIButton buttonWithType:UIButtonTypeCustom]; 
+0

不會阻止文本? – user3157910 2014-10-06 00:52:33

+0

好吧,我更新了我的文章,因爲我沒有意識到文本不是圖像的一部分。該按鈕只需要初始化以鍵入自定義。 – 2014-10-06 01:23:37

+0

這個技巧。謝謝 – user3157910 2014-10-06 01:47:29