2009-07-14 49 views
2

我試着用UIButtonTypeCustom創建一個UIButton。然後事後用以下方法來調整它如何創建自定義UIButton(帶圖像)但仍使用setTitle?

[sendButton setImage:[UIImage imageNamed:@"sendButtonOff.png"] forState:UIControlStateNormal]; 
[sendButton setImage:[UIImage imageNamed:@"sendButtonOn.png"] forState:UIControlStateHighlighted];  
[sendButton setImage:[UIImage imageNamed:@"sendButtonDisabled.png"] forState:UIControlStateDisabled]; 

外觀然而問題是,一旦你開始設置UIImages的狀態,沒有的setTitle不再工作。我認爲setTitle只適用於非UIButtonTypeCustom?這意味着我要麼必須將文本放入圖像本身(不是非常健壯),要麼將其子類化,並將UILabel添加到視圖中?聽起來像一個大量的工作:(任何想法

回答

10

如果你想要的標題顯示在圖像上,嘗試使用此:

[sendButton setBackgroundImage:[UIImage imageNamed:@"sendButtonOff.png"] forState:UIControlStateNormal]; 
[sendButton setBackgroundImage:[UIImage imageNamed:@"sendButtonOn.png"] forState:UIControlStateHighlighted];  
[sendButton setBackgroundImage:[UIImage imageNamed:@"sendButtonDisabled.png"] forState:UIControlStateDisabled]; 
+0

謝謝,這helpd .. !! – 2012-07-07 11:29:02