IOS代码段【不让键盘挡住输入框】
来源:互联网 发布:python常用内置函数 编辑:程序博客网 时间:2024/04/29 10:18
-(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]; } } }