1 #pragma mark 键盘弹出遮挡输入框
2
3 //开始编辑输入框的时候,软键盘出现,执行此事件
4 -(void)textFieldDidBeginEditing:(UITextField *)textField
5 {
6 CGRect frame = textField.frame;
7 int offset = frame.origin.y + 72 - (self.view.frame.size.height - 216.0);//键盘高度216
8
9 NSTimeInterval animationDuration = 0.30f;
10 [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
11 [UIView setAnimationDuration:animationDuration];
12
13 //将视图的Y坐标向上移动offset个单位,以使下面腾出地方用于软键盘的显示
14 if(offset > 0)
15 self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
16
17 [UIView commitAnimations];
18 }
19
20 //当用户按下return键或者按回车键,keyboard消失
21 -(BOOL)textFieldShouldReturn:(UITextField *)textField
22 {
23 [textField resignFirstResponder];
24 return YES;
25 }
26
27 //输入框编辑完成以后,将视图恢复到原始状态
28 -(void)textFieldDidEndEditing:(UITextField *)textField
29 {
30 self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
31 }