2014-10-01 59 views
0

的背景顏色,我創造了我所使用的用戶registration.In註冊頁面的應用程序,我已經創造了許多UITextFields.I希望,當用戶選擇的任何文本字段那麼它的背景圖像的變化。我使用的代碼是這樣做的:更改所選文本字段

- (IBAction)touchBegin:(id)sender 
{ 
    //UILabel *opt = (UILabel *)[cell viewWithTag:101]; 
    if (_text1.isSelected) { 
     _text1.background = [UIImage imageNamed:@"big_star_non_rating.png"]; 
    } 
    else if (_text2.isSelected){ 
     _text2.background = [UIImage imageNamed:@"big_star_non_rating.png"]; 
    } 
    else if (_text3.isSelected){ 
     _text3.background = [UIImage imageNamed:@"big_star_non_rating.png"]; 
    } 
    else if (_text4.isSelected){ 
     _text4.background = [UIImage imageNamed:@"big_star_non_rating.png"]; 
    } 
    else{ 

    } 
} 

但我沒有得到根據我的需要的結果。 我應該用什麼來獲得正確的輸出?

+0

什麼是您的文本字體樣式?邊框風格還是什麼? – Maul 2014-10-01 10:01:31

回答

1

確保您的文本框的邊框樣式是否UITextBorderStyleNone。

yourTextField.borderStyle = UITextBorderStyleNone; 

yourTextField.background = [UIImage imageNamed:@"image.png"]; 
0

嘗試這個

圖像:

[_text1 setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"image.png"]]]; 

or 

_text1.background = [UIImage imageNamed:@"image.png"]; 

對於顏色:

[_text1 setBackgroundColor:[UIColor blackColor]]; 
0

我認爲這是更好的和更容易使用UITextFieldDelegate方法是這樣,例如:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    textField.background = [UIImage imageNamed:@"big_star_non_rating"]; 
    return YES; 
} 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
    textField.background = [UIImage imageNamed:@"AnImageForNormalStateOrSetNilBackground"]; 
    return YES; 
} 

希望它有幫助。

+0

在我的代碼中,if條件無法正常工作。我應該使用哪種If/switch條件來獲得結果(當選擇TextField時,其顏色不會改變其他顏色,我想知道正確的if/switch條件以獲得輸出結果 – 2014-10-01 10:52:08

+0

在這種方法中,您不需要將一個if /切換條件,在這個方法中接收到的'textfield'(UITextfield *)是正在編輯的文本字段。 – 2014-10-01 15:47:46