ios 评论或者回复 键盘定位问题

浅谈下个人关于朋友圈点击评论 或者点击回复行键盘定位问题、键盘切换以及第三方键盘接入的问题;

一:首先是键盘定位问题

系统关于keyboard弹出监听有以下几个通知(根据字面意思可以理解一下):

 UIKeyboardWillShowNotification

 UIKeyboardDidShowNotification

UIKeyboardWillHideNotification

UIKeyboardDidHideNotification

UIKeyboardWillChangeFrameNotification //键盘Frame改变的通知

UIKeyboardDidChangeFrameNotification

UIKeyboardWillShowNotification,UIKeyboardWillHideNotification这个两个通知一般搭配使用,但是在键盘切换或者第三方键盘接入的时候效果不是很好,所以推崇使用UIKeyboardWillChangeFrameNotification这个通知,可以检测键盘弹出,收回,键盘切换或者第三方键盘如搜狗接入后键盘的实时变化


二:需要定位的视图是否是当前子视图

       (1)如果是当前子视图只需要在键盘做出改变的时候监听即可:


主要是判断键盘的坐标 在所需要定位视图的上方还是下方。

    (2)如果当前需要定位的视图(按钮或者评论行)不是当前视图的直接子视图,比如这样:


如果针对cell的label所在行进行定位,所以需要把cell中的评论按钮或者评论行转换到当前视图或者window中:self.history_Y_offset = [cell.contentLabel convertRect:cell.contentLabel.bounds toView:window].origin.y;

转换坐标可以借鉴这篇文章:坐标转换

三:键盘切换或者第三方键盘接入问题处理:

if (self.tableview.isDragging) {

return;

}

NSDictionary *userInfo = notification.userInfo;

// 动画的持续时间

double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

// 键盘的frame

CGRect keyboardRect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

CGFloat keyboardHeight = keyboardRect.size.height;

CGFloat delta = 0.0;

delta = self.history_Y_offset - ([UIApplication sharedApplication].keyWindow.bounds.size.height - keyboardHeight-40-1);//

CGPoint offset = self.tableview.contentOffset;

offset.y += delta;

if (offset.y < 0) {

offset.y = 0;

}

[self.tableview setContentOffset:offset animated:YES];

MyCell *cell =[self.tableview cellForRowAtIndexPath:self.selectedIndexPath];

UIWindow *window =[UIApplication sharedApplication].keyWindow;

self.history_Y_offset = [cell.contentLabel convertRect:cell.contentLabel.bounds toView:window].origin.y;//在设置完contentoffset后必须重新设置所需定位视图的contentoffset,因为在键盘切换过程中,该视图的contentoffset会发生变化,切换到搜狗键盘也会发生变化;

demo 地址:评论或者回复 键盘定位问题 

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容