怎样实现让输入框随着键盘上浮或者返回

想实现如同聊天界面的输入框随着键盘移动的效果,如图:
输入框岁键盘移动

轻拍后返回原来的位置:

输入框返回

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];


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


}
//返回的手势
- (void)tapA{
    [self.textField resignFirstResponder];
}

- (void)keyboardWillShow:(NSNotification *)notification {

    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.textField convertRect:keyboardRect fromView:nil];

    // 根据老的 frame 设定新的 frame
    CGRect newTextViewFrame = self.textField.frame; // by michael
    newTextViewFrame.origin.y = keyboardRect.origin.y - self.textField.frame.size.height;

    // 键盘的动画时间,设定与其完全保持一致
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];

    // 键盘的动画是变速的,设定与其完全保持一致
    NSValue *animationCurveObject = [userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
    NSUInteger animationCurve;
    [animationCurveObject getValue:&animationCurve];

    // 开始及执行动画
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];
    self.textField.frame = newTextViewFrame;
    [UIView commitAnimations];

}

//键盘消失时的处理,文本输入框回到页面底部。

- (void)keyboardWillHide:(NSNotification *)notification {

    NSDictionary* userInfo = [notification userInfo];

    // 键盘的动画时间,设定与其完全保持一致
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];

    // 键盘的动画是变速的,设定与其完全保持一致
    NSValue *animationCurveObject =[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey];
    NSUInteger animationCurve;
    [animationCurveObject getValue:&animationCurve];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];
    CGRect newTextViewFrame = self.textField.frame;

    //获取当前屏幕的高度
    NSInteger DEVICE_HEIGHT = [UIScreen mainScreen].bounds.size.height;

    newTextViewFrame.origin.y = DEVICE_HEIGHT - self.textField.frame.size.height;

    self.textField.frame = newTextViewFrame;
    [UIView commitAnimations];

}

注意:如果你的输入框是xib中已经加过约束的,输入框不会跟着键盘移动,那么将以上代码中 keyboardWillShow 和 keyboardWillHide 方法 中的所有的 self.textField 改为 self.view 就可以啦。然后不是xib拖拽的,代码添加的输入框,也可能不是想要的结果,就用同样的处理方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值