UITextField losing firstResponder returns animation / modification frames

I am using the following code to resize the frame to accommodate the keyboard showing / disappearing:

- (void)moveCodeViewForKeyboard:(NSNotification*)aNotification up:(BOOL)up {
    NSDictionary* userInfo = [aNotification userInfo];
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardEndFrame;

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];

    CGRect newFrame = codeView.frame;
    CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];
    keyboardFrame.size.height += keyboardToolbar.frame.size.height;
    keyboardFrame.size.height += 10;
    newFrame.size.height -= keyboardFrame.size.height * (up?1:-1);
    codeView.frame = newFrame;

    [UIView commitAnimations];
}

      

This code is repeated for some of the other subzones that animate the displayed / hidden keyboard.

When clicking on the UITextField, all animations are displayed correctly. But if I immediately touch the UITextView, all the elements (UIToolbar, UITextView, UIWebView) that were animated earlier return to their original frames (position and size) and no longer animate. If I disable the keyboard and the UITextField is firstResponder, the items go back to their original frame, but animate again.

Very strange...

Just because I think people will ask:

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"textFieldDidEndEditing");
}

      

Thanks in advance!

+3


source to share


1 answer


Is it used in your project autolayout

? If so, constraints are automatically created for you when you use the Interface Builder, and it is not enough just to change the frameworks of the components, you need to adjust the constraints you have on those components. You usually do this by binding constraints to IBOutlet

properties and changing the property constant

to them.



0


source







All Articles