-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
UITapGestureRecognizer*tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(done:)];
tapGestureRecognizer.numberOfTapsRequired =1;
[self.view addGestureRecognizer:tapGestureRecognizer]; //只需要点击非文字输入区域就会响应hideKeyBoard
[tapGestureRecognizer release];
[UIViewbeginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:0.3f];
float width=self.view.frame.size.width;
float height=self.view.frame.size.height;
CGRectrect=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:[UITextFieldclass]]) {
[view resignFirstResponder];
[UIViewbeginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:0.3f];
floatwidth=self.view.frame.size.width;
floatheight=self.view.frame.size.height;
CGRectrect=CGRectMake(0.0f,0.0f,width,height);
self.view.frame=rect;
[UIView commitAnimations];
}
}
}
IOS代码段【不让键盘挡住输入框】
最新推荐文章于 2022-09-04 16:42:17 发布