我使用以下代码来修改框架大小以适应键盘出现/消失:
- (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];
}
对于在显示/隐藏键盘时进行动画处理的其他一些子视图,重复此代码。
点击 UITextField 可使所有动画正确显示。但是,如果我随后立即点击 UITextView,之前动画的所有元素(UIToolbar、UITextView、UIWebView)都会恢复到它们的原始帧(位置和大小)并且将不再动画。如果我在 UITextField 为 firstResponder 时关闭键盘,则元素将返回到原始帧,但会再次设置动画。
很诡异...
只是因为我认为人们会问:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"textFieldDidEndEditing");
}
提前致谢!