簡體   English   中英

更改所選TextField的背景顏色

[英]Change the background color of selected TextField

我正在創建一個使用用戶注冊的應用程序。在注冊頁面中,我創建了許多UITextFields。我希望當用戶選擇任何TextField時其背景圖像都會更改。 我使用代碼來做到這一點:

- (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{

    }
}

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

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

yourTextField.borderStyle = UITextBorderStyleNone;

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

我認為使用這樣的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;
}

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM