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 地址:評論或者回復 鍵盤定位問題?

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容