Search code examples
objective-cuitextfieldcore-animation

Animate size of UITextField when pressed


I have a text field that I want to change the width and image of when I touch inside it. The image changes without any issues but the width remains the same. The issue somehow lies in the action triggered when touching the text field because when I create a simple button the animation works. So my question is how to get the animation working when touching the text field.

The animation doesn't work here:

- (void)textFieldDidBeginEditing:(UITextField *)textField{

        [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationCurveEaseOut
                         animations:^{

                         self.textFieldSearchStore.background = [UIImage imageNamed:@"text-field-small.png"];
                         self.textFieldSearchStore.frame = CGRectMake(textFieldSearchStore.frame.origin.x, textFieldSearchStore.frame.origin.y, 248.0f, textFieldSearchStore.frame.size.height);

                     }
                     completion:^(BOOL finished){}
     ];

}

The animation works here:

- (IBAction)test:(id)sender {

    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationCurveEaseOut
                     animations:^{

                         self.textFieldSearchStore.background = [UIImage imageNamed:@"text-field-small.png"];
                         self.textFieldSearchStore.frame = CGRectMake(textFieldSearchStore.frame.origin.x, textFieldSearchStore.frame.origin.y, 248.0f, textFieldSearchStore.frame.size.height);

                     }
                     completion:^(BOOL finished){}
     ];

}

Solution

  • Problem solved. The issue was that I was using a Table View Controller. Changed to a regular View Controller and now it's working!