简体   繁体   中英

UITextField losing firstResponder reverts frame animations/modifications

I use the following code to modify the frame size to accomodate a keyboard showing up/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 other subviews that animate upon the keyboard being shown/hidden.

Tapping a UITextField makes all the animations appear properly. But if I then immediately tap to a UITextView, all the elements (UIToolbar, UITextView, UIWebView) that had animated previously revert to their original frames (position and size) and will no longer animate. If I dismiss the keyboard while the UITextField is firstResponder, the elements return to original frame but will animate again.

Very bizarre...

Just because I think people will ask:

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

Thanks in advance!

Is your project set to use autolayout ? If it is then constraints are being created automatically for you while you are using Interface Builder, and it is not sufficient to just change the frames of the components anymore, you need to adjust the constraints you have on those components. You do that usually by linking the constraints to IBOutlet properties and modify the constant property on them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM