0

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...

gtmtg
  • 3,010
  • 2
  • 22
  • 35

1 Answers1

1

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];
}
Tobi
  • 5,499
  • 3
  • 31
  • 47
  • Yeah - the thing is, this was working before I enabled Auto-Layout... At first I thought it was a problem with the constraints, but the first text field works perfectly if it is the first one to be focused... – gtmtg Nov 04 '12 at 20:27
  • 1
    If your using autolayout you can't change the size of the text field by changing their frames. You have the setup a width constraint for example and modify that. – Tobi Nov 04 '12 at 20:38
  • yes that is strange, but since under autolayout the frames are calculated by the system in regard to the set up constraints you can't modify them manually (if it's working sometimes, that seems to be a bug). Anyway the fact is that to modify any layout while using autolayout you have to modify the appropriate constraints. – Tobi Nov 04 '12 at 20:47
  • That does work - thanks... Is there any way to animate that, though? – gtmtg Nov 04 '12 at 20:51
  • I'll update my answer, it's a little hard to write this in the comment – Tobi Nov 04 '12 at 21:02
  • That's fine - [this question](http://stackoverflow.com/questions/12622424/how-do-i-animate-constraint-changes) describes animating constraint changes... I'll mark your answer as accepted because changing the constraints is working. – gtmtg Nov 04 '12 at 21:10