iOS中输入框随键盘上下移动

作者: 逆世界开发者 | 来源:发表于2016-09-21 17:39 被阅读763次

    1.在初始化方法里边注册键盘坐标移动的通知

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];//监听键盘的坐标的变化

    2,实现输入框随键盘移动的方法

    -(void)keyboardChangeFrame:(NSNotification*)notification

    {

    //从通知中取出动画速率

    intcurve = [[notification.userInfoobjectForKey:UIKeyboardAnimationCurveUserInfoKey]intValue];

    //取出动画时间

    floatduration = [[notification.userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey]floatValue];

    //取出键盘动画结束后键盘的frame

    CGRectendFrame = [[notification.userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];

    [UIViewanimateWithDuration:durationanimations:^{

    [UIViewsetAnimationCurve:curve];

    _commentView.frame=CGRectMake(0, endFrame.origin.y-H_i6real(38),self.view.frame.size.width,H_i6real(38));

    _commentTableView.frame=CGRectMake(W_i6real(10), endFrame.origin.y-(self.view.frame.size.width/2)-H_i6real(10),self.view.frame.size.width-W_i6real(80+10),self.view.frame.size.width/2-H_i6real(38));

    }];

    }

    相关文章

      网友评论

      • 白菜松:设置下textfield的inputaccessoryview不就行了么?
        逆世界开发者:@白菜松 我没有用过那个属性,谢谢指导!

      本文标题:iOS中输入框随键盘上下移动

      本文链接:https://www.haomeiwen.com/subject/qwmaettx.html