键盘挡住UITextField,且点击非输入框时隐藏键盘

 

在键盘弹出时先添加偏移,有多个输入框时可通过设置tag来区别:

 

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(done:)]; tapGestureRecognizer.numberOfTapsRequired = 1; [self.view addGestureRecognizer: tapGestureRecognizer]; //只需要点击非文字输入区域就会响应hideKeyBoard [tapGestureRecognizer release]; [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:0.3f]; float width=self.view.frame.size.width; float height=self.view.frame.size.height; CGRect rect=CGRectMake(0.0f,-80*(textField.tag),width,height);//上移80个单位,按实际情况设置 self.view.frame=rect; [UIView commitAnimations]; return YES; } -(void)done:(id)sender { for (UIView *view in self.view.subviews) { if ([view isKindOfClass:[UITextField class]]) { [view resignFirstResponder]; } } [UIView beginAnimations:@"ResizeForKeyboard" context:nil]; [UIView setAnimationDuration:0.3f]; float width=self.view.frame.size.width; float height=self.view.frame.size.height; CGRect rect=CGRectMake(0.0f,0.0f,width,height);//上移80个单位,按实际情况设置 self.view.frame=rect; [UIView commitAnimations]; }


或在

- (BOOL)textFieldShouldReturn:(UITextField *)textField { return YES; } 

添加响应也行.

 

你可能感兴趣的:(iOS)