ios--解决键盘遮挡UITextField控件的问题(方式一)

参考资料:http://blog.csdn.net/ryantang03/article/details/8203605

主要功能包括:

  • 自适应键盘出现后View的高度调整,防止遮挡输入框
  • 点击背景区域关闭键盘
  • 响应键盘上Return按钮事件(实现在上下UITextFiled间切换光标)
1.首先在ViewController中实现UITextField的一个Delegate

#import <UIKit/UIKit.h>

@interface AloneSetSiteViewController : UIViewController<UITextFieldDelegate>
{
}
@property(nonatomic,retain) NSMutableDictionary *dictionary;
@property(nonatomic,retain) IBOutlet UITextField *txtName,*txtPerson,*txtCircle,*txtPassWd,*txtConfirmPassWd;
@property(nonatomic,retain) IBOutlet UIScrollView *scroller;
-(IBAction)backOff:(id)sender;
-(IBAction)nextStep:(id)sender;
@end

2.实现UITextFiledDelegate中的协议方法

//UITextField的协议方法,当开始编辑时监听
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    float upDist = 0.0f;
    if (textField == txtCircle) {
        upDist = -40.0f;
    }
    else if (textField == txtPassWd) {
        upDist = -120.0f;
    }
    else if(textField == txtConfirmPassWd)
    {
        upDist = -200.0f;
    }
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.view.frame.size.width;
    float height = self.view.frame.size.height;
    //上移30个单位,按实际情况设置
    CGRect rect=CGRectMake(0.0f,upDist,width,height);
    self.view.frame=rect;
    [UIView commitAnimations];
    return YES;
}

3.恢复移动的视图的方法

//恢复原始视图位置
-(void)resumeView
{
    NSTimeInterval animationDuration=0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    float width = self.view.frame.size.width;
    float height = self.view.frame.size.height;
    //如果当前View是父视图,则Y为20个像素高度,如果当前View为其他View的子视图,则动态调节Y的高度
    float Y = 0.0f;
    CGRect rect=CGRectMake(0.0f,Y,width,height);
    self.view.frame=rect;
    [UIView commitAnimations];
}

4.点击背景隐藏键盘及响应键盘上Return按键的方法

//隐藏键盘的方法
-(void)hidenKeyboard
{
    [self.txtName resignFirstResponder];
    [self.txtCircle resignFirstResponder];
    [self.txtPerson resignFirstResponder];
    [self.txtPassWd resignFirstResponder];
    [self.txtConfirmPassWd resignFirstResponder];
    [self resumeView];
}

//点击键盘上的Return按钮响应的方法
-(IBAction)nextOnKeyboard:(UITextField *)sender
{
    if(sender == self.txtName) {
        [self.txtPerson becomeFirstResponder];
    }
    else if(sender == self.txtPerson) {
        [self.txtCircle becomeFirstResponder];
    }
    else if(sender == self.txtCircle) {
        [self.txtPassWd becomeFirstResponder];
    }
    else if (sender == self.txtPassWd) {
        [self.txtConfirmPassWd becomeFirstResponder];
    }
    else if (sender == self.txtConfirmPassWd){
        [self hidenKeyboard];
    }
}

5.指定协议及注册事件

    /// 注册键盘事件
    //指定本身为代理
    self.txtName.delegate = self;
    self.txtCircle.delegate = self;
    self.txtPerson.delegate = self;
    self.txtPassWd.delegate = self;
    self.txtConfirmPassWd.delegate = self;
    //指定编辑时键盘的return键类型
    self.txtName.returnKeyType = UIReturnKeyNext;
    self.txtCircle.returnKeyType = UIReturnKeyNext;
    self.txtPerson.returnKeyType = UIReturnKeyNext;
    self.txtPassWd.returnKeyType = UIReturnKeyNext;
    self.txtConfirmPassWd.returnKeyType = UIReturnKeyDefault;
    //注册键盘响应事件方法
    [self.txtName addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.txtCircle addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.txtPerson addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.txtPassWd addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [self.txtConfirmPassWd addTarget:self action:@selector(nextOnKeyboard:) forControlEvents:UIControlEventEditingDidEndOnExit];
    
    //添加手势,点击屏幕其他区域关闭键盘的操作
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hidenKeyboard)];
    gesture.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:gesture];

最后运行工程,效果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值