//开始编辑输入框的时候,软键盘出现,执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect frame = [textField convertRect:textField.frame toView:self.view];
//上移数值,根据自己情况调整
int offset = frame.origin.y - 100 - (UISCROEEN_SIZE.height - 216.0);//键盘高度216
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
//将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
if(offset > 0)
self.view.frame = CGRectMake(0.0f, -offset, UISCROEEN_SIZE.width, UISCROEEN_SIZE.height);
[UIView commitAnimations];
}
//输入框编辑完成以后,将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
self.view.frame = CGRectMake(0, 0, UISCROEEN_SIZE.width, UISCROEEN_SIZE.height);
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}