Change UITextField background when editing begins

后端 未结 4 1506
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 11:12

I\'d like to change the background image of a UITextField when it becomes the firstResponder to show the user that it has focus, similar to the :active or :focus pseudo-clas

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 11:34

    The cleanest way IMHO is to subclass UITextField and override becomeFirstResponder and resignFirstResponder to change the background image of the text field. That way, you can use your new subclass anywhere without having to reimplement the delegate methods to change the background.

    - (BOOL)becomeFirstResponder {
        BOOL outcome = [super becomeFirstResponder];
        if (outcome) {
          self.background = // selected state image;
        }
        return outcome;
    }
    
    - (BOOL)resignFirstResponder {
        BOOL outcome = [super resignFirstResponder];
        if (outcome) {
          self.background = // normal state image;
        }
        return outcome;
    }
    

提交回复
热议问题