码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-12-12 18:48:37      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   sp   on   div   log   bs   cti   

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];
    
    
}

 搞定

 

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

标签:blog   io   ar   sp   on   div   log   bs   cti   

原文地址:http://www.cnblogs.com/angongIT/p/4160217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!