gtmtg
gtmtg

Reputation: 3030

UITextField Won't Change Frame on Refocus

I'm having a peculiar problem. I have a view with two UITextFields that start out 280px wide. On focus, I want them to shorten to reveal a button - I'm doing that with the following code:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect revealButton = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 221, textField.frame.size.height);

    [UIView beginAnimations:nil context:nil];
    textField.frame = revealButton;
    [UIView commitAnimations];
    NSLog(@"%f",textField.frame.size.width);
}

Once editing has ended, they should go back to their original frame:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CGRect hideButton = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, 280, textField.frame.size.height);

    [UIView beginAnimations:nil context:nil];
    textField.frame = hideButton;
    [UIView commitAnimations];
}

The first time I focus a text field, it works perfectly. However, if I focus the first text field after focusing something else (for example, if I focus the first text field initially, focus the second, and then refocus the first, or if I initially focus the second and then focus the first), it simply won't change its frame. Even more puzzling is the fact that it will log 221 as its width - it just won't show that on the screen. Furthermore, this problem doesn't apply to the second text field.

Any ideas? Thanks in advance...

Upvotes: 0

Views: 686

Answers (1)

Tobi
Tobi

Reputation: 5519

That's strange, I ran a quick test using two text fields with the exact same code and works every time.

I'd suggest deleting the text fields and connections and rebuild them. Clean all targets and try again.

Edit according to your comments:

If you're using Auto Layout you must not modify the frame of the text fields directly. The actual frames of UI elements are calculated by the system.

For your purpose I'd suggest to set up a width constraint for every text field. Make sure that you only have a left or right spacing constraint not both in addition to the width constraint. To animate it use the following code:

- (NSLayoutConstraint *)widthConstraintForView:(UIView *)view
{
    NSLayoutConstraint *widthConstraint = nil;

    for (NSLayoutConstraint *constraint in textField.constraints)
    {
        if (constraint.firstAttribute == NSLayoutAttributeWidth)
            widthConstraint = constraint;
    }

    return widthConstraint;
}

- (void)animateConstraint:(NSLayoutConstraint *)constraint toNewConstant:(float)newConstant withDuration:(float)duration
{
    [self.view layoutIfNeeded];
    [UIView animateWithDuration:duration animations:^{
        constraint.constant = newConstant;
        [self.view layoutIfNeeded];
    }];
}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    float newWidth = 221.0f;

    NSLayoutConstraint *widthConstraint = [self widthConstraintForView:textField];

    [self animateConstraint:widthConstraint toNewConstant:newWidth withDuration:0.5f];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    float newWidth = 280.0f;

    NSLayoutConstraint *widthConstraint = [self widthConstraintForView:textField];

    [self animateConstraint:widthConstraint toNewConstant:newWidth withDuration:0.5f];
}

Upvotes: 1

Related Questions