iOS 即时聊天键盘处理

1 让tableview 滚动到最底层方法

if (_dataArr.count>0) {

        NSIndexPath *ip = [NSIndexPath indexPathForRow:_dataArr.count-1 inSection:0];

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:YES];

        });

    }

2了解scrollview一个方法,用手指滑动才会执行的方法

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 

3每次聊天要在最下面加一条数据 使用到的方法(不能用刷新方法加最后一条 得插入)

NSInteger index = self.dataArr.count-1;

  NSIndexPath *indexPath=[NSIndexPath indexPathForRow:index inSection:0];

  [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

4 刷新某一条cell方法

  NSIndexPath *indexPath=[NSIndexPath indexPathForRow:index inSection:0];

     [_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

5 获取当前tableview 内容高度 有几种方法

(1)获取section的frame

- (CGRect)rectForSection:(NSInteger)section;

(2)获取cell的frame

- (CGRect)rectForRowAtIndexPath:(NSIndexPath *)indexPath;

(3)获取header的frame

- (CGRect)rectForHeaderInSection:(NSInteger)section;

(4)获取footer的frame

- (CGRect)rectForFooterInSection:(NSInteger)section;

***********这里边 我们需求只有一个section 写一个footer 来获取整个当前tableview内容高度

 CGRect rectCell = [_tableView rectForFooterInSection:0];

当前内容高度为:rectCell.origin.y

6 以上五部结合一下 弹键盘,适用场景,打一次字,发出去收键盘 ,要是想持续打字键盘不收回去,还需要优化一下下面的方法

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

    CGRect rect=[notification.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];

    WS(weakSelf)

    [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {

        make.bottom.equalTo(self.view).offset(-rect.size.height);

    }];

    if (Height(85)+rect.size.height +weakSelf.contentHeight>kScreenHeight) {

        if (weakSelf.contentHeight>kScreenHeight-Height(85)-45) {

            [UIView animateWithDuration:0.25 animations:^{

                CGRect frame = weakSelf.tableView.frame;

                frame.origin.y = - (rect.size.height)+Height(85);

                weakSelf.tableView.frame = frame;

            }];

            [UIView animateWithDuration:0.25 animations:^{

                [weakSelf.tableView refreshCurrentTableViewContengHeight];

            }];

        }else{

            [UIView animateWithDuration:0.25 animations:^{

                CGRect frame = weakSelf.tableView.frame;

                frame.origin.y = - ( Height(85)+rect.size.height +weakSelf.contentHeight)+(kScreenHeight-Height(85))+Height(85)+45;

                weakSelf.tableView.frame = frame;

            }];

            [UIView animateWithDuration:0.25 animations:^{

                [weakSelf.tableView refreshCurrentTableViewContengHeight];

            }];

        }

    }

}

 

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

    CGFloat tabbarHeight = Height_TabbarSafeBottom;

    WS(weakSelf)

    [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {

        make.bottom.equalTo(self.view).offset(-tabbarHeight);

    }];

        [UIView animateWithDuration:0.25 animations:^{

            CGRect frame = weakSelf.tableView.frame;

            frame.origin.y = Height(85);

            weakSelf.tableView.frame = frame;

        }];

}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值