3

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!

Jason Pawlak
  • 792
  • 1
  • 6
  • 20
  • After playing with it more, looks like it is not so much related to the animation as it is related to modifying a UIView (or subclass of) frame value (size or location). There must be some setting on a parent view that does not allow for persistence... possibly? – Jason Pawlak Feb 01 '13 at 02:14

1 Answers1

0

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.

shipmaster
  • 3,994
  • 4
  • 30
  • 33