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 toIBOutlet
properties and modify theconstant
property on them.