首页 > 代码库 > 比较简单的解决键盘遮挡文本框的简单方法

比较简单的解决键盘遮挡文本框的简单方法

1、首先需要在控制器的view中添加上一个scrollView,并遵循UITextFieldDelegate代理,并设置文本框的自身代理

2、添加手势监听

     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTouch:)];    [self.scrollerView addGestureRecognizer:tap];

 

- (void)tapTouch:(UIGestureRecognizer*)gesture{        [self.view endEditing:YES];    [self.userName resignFirstResponder];    [self.passWord resignFirstResponder];    [self.checkPassWord  resignFirstResponder];    [self.phoneNumber resignFirstResponder];    [self textFieldDidBeginEditing:0];}

 3、在代理方法中设置相应比较合适的代码

-(void)textFieldDidBeginEditing:(UITextField *)textField{    CGRect rect = self.scrollerView.frame;    [UIView beginAnimations:nil context:NULL];    [UIView setAnimationDuration:0.3];        if (textField == self.userName)    {        rect.origin.y = 0;    }else if (textField == self.passWord)    {        rect.origin.y = -50;    }    else if (textField == self.checkPassWord)    {        rect.origin.y = -80;    }    else if (textField == self.phoneNumber)    {        rect.origin.y = -100;    }else    {        rect.origin.y = 0;    }    self.scrollerView.frame = rect;    [UIView commitAnimations];        }

 搞定

 

比较简单的解决键盘遮挡文本框的简单方法