Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UITextField background when editing begins

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-classes in CSS.

I'm guessing that I may need to do this programmatically; so any help is greatly appreciated.

-Giles

like image 654
Giles Van Gruisen Avatar asked Jan 03 '10 01:01

Giles Van Gruisen


3 Answers

SWIFT 4

textField.addTarget(self, action: #selector(anyFunction), for: UIControlEvents.editingDidBegin)


@objc func anyFunction() {
    // Add your conde here
}
like image 52
Wissa Avatar answered Oct 05 '22 15:10

Wissa


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;
}
like image 29
Ziconic Avatar answered Oct 05 '22 14:10

Ziconic


You might as well use the UITextFieldDelegate methods (IMHO, easier to maintain than key-value observers):

#pragma mark -
#pragma mark UITextFieldDelegate methods

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

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

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

This only works when the UITextField.borderStyle property is of any type but UITextBorderStyleRoundedRect (in that case, the background property is not taken into account). This means you might use the code above with UITextBorderStyleBezel, UITextBorderStyleLine and UITextBorderStyleNone, as explained in the borderStyle documentation:

borderStyle

The border style used by the text field.

@property(nonatomic) UITextBorderStyle borderStyle

Discussion

The default value for this property is UITextBorderStyleNone. If a custom background image is set, this property is ignored.

This is the documentation for the background property of UITextField:

background

The image that represents the background appearance of the text field when it is enabled.

@property(nonatomic, retain) UIImage *background

Discussion

When set, the image referred to by this property replaces the standard appearance controlled by the borderStyle property. Background images are drawn in the border rectangle portion of the image. Images you use for the text field’s background should be able to stretch to fit.

like image 40
Adrian Kosmaczewski Avatar answered Oct 05 '22 14:10

Adrian Kosmaczewski