解决tableview 列表键盘弹起输入框遮挡cell问题


self.keyBorldH. 是获取的键盘的高度   50 是输入框的高度  64 是导航栏的高度

采取的方案
1.=====当键盘弹起时候,,手动设置tableview的frame为导航栏到键盘上端的区域。。

self . tableView . frame = CGRectMake ( 0 , 0 , Width , Height - 64 -50 - self . keyBorldH );

2.===== 键盘回收时,,手动设置tableview的frame为导航栏到屏幕低端获取点击的cell在屏幕中的位置

self . tableView . frame = CGRectMake ( 0 , 0 , Width , Height - 64 -50 );

3.获取点击的cell的挨着的下一条cell在屏幕中的起始位置,,与输入框的起始位置做比较,,如果挨着的下一条cell的起始位置大于输入框的起始位置(即当前cell的低端),,则认为当前点击的cell会被键盘遮挡,,则当前点击的cell偏移到tableview的最低端(即键盘的上方)。。

  ///// 获取点击的cell的下一个cell在屏幕中的位置。。
        CGRect rectInTableView = [tableView rectForRowAtIndexPath :[ NSIndexPath indexPathForRow :indexPath. row + 1 inSection :indexPath. section ]];
        CGRect rect = [tableView convertRect :rectInTableView toView :[tableView superview ]];
        dispatch_time_t delayTime = dispatch_time ( DISPATCH_TIME_NOW , ( int64_t )( 0.5 /*延迟执行时间*/ * NSEC_PER_SEC));
        dispatch_after (delayTime, dispatch_get_main_queue (), ^{
            /// 点击的cell的下一个cell的起始位置与输入框比对,,是否覆盖,,会覆盖则点击的cell偏移到键盘上方,,也是tableview的最低端(注意tableview的frame),,不会覆盖就不偏移

 if (indexPath.row == self.pingLunArray.count-1) {

                /// 最后一个cell直接偏移

                [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

            }else{

                if (rect.origin.y>self.keyBordView.origin.y) {

                    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

                }

            }


        });











猜你喜欢

转载自blog.csdn.net/flg1554112450/article/details/79881644