2011-11-03 81 views
6

我定製UIButton編程這裏:正在按下UIButton的UIControlState是什麼?

button = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [button setSelected:YES]; 
     button.frame = CGRectMake(x, y, width, height); 
     [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
     [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected]; 
     [button setTitle:@"Button Title" forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
     [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal]; 
     [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateSelected]; 
     [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted]; 
     [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateDisabled]; 

的問題是,如果我按住圖像背景圖像中消失,直到我放開了它...

回答

8

我覺得你在矯枉過正的模式:)。嘗試爲UIControlStateNormal設置button.png,爲UIControlStateHighlighted設置buttonActive.png。沒有必要休息。看看這是否有效。

編輯:

此外,請記住:圖像文件的名稱是區分

你在設備上測試是否敏感?映像名稱對於設備版本是區分大小寫的,但不適用於模擬器。例如,如果您的實際圖像文件名爲buttonactive.png,但您將代碼稱爲buttonActive.png,則它將顯示在模擬器上,但不顯示在設備上。

請確保兩個圖像名稱的大小寫與實際文件的名稱相匹配。

編輯#2:

試試這個代碼

button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [button setSelected:YES]; 
    button.frame = CGRectMake(x, y, width, height); 
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected]; 
    [button setTitle:@"Button Title" forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [button setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal]; 
    [button setBackgroundImage:[UIImage imageNamed:@"buttonActive.png"] forState:UIControlStateHighlighted]; 
+1

我想補充一點,圖像文件的名稱是區分上一個敏感設備,但不在模擬器中。這讓我撓了腦袋幾次:) – Keller

+0

@Sid:我知道**圖像名稱區分大小寫**。我在iPhone模擬器上測試,但應該反應相同... –

+0

@Keller::) ... – Sid

3

圖出來,它的工作過程是這樣:

[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:@"ActivateButton.png"] forState:UIControlStateSelected]; 
[_whateverButtonTab setBackgroundImage:[UIImage imageNamed:@"ActivateButton.png"] forState:(UIControlStateHighlighted|UIControlStateSelected)]; 
-2

雖然我們從IDE

添加一個按鈕樣品:

.m文件10

它是

-(IBAction)BtnAdd:(id)sender 
{ 
} 

它是不能啓用或禁用的方法。

所以如果你想啓用或禁用按鈕,使它作爲-(IBOutlet)BtnAdd 添加IBOutlet.h文件,並將其連接到特定的按鈕 然後BtnAdd.enabled=NO將工作