淺談下個人關(guān)于朋友圈點(diǎn)擊評論 或者點(diǎn)擊回復(fù)行鍵盤定位問題、鍵盤切換以及第三方鍵盤接入的問題;
一:首先是鍵盤定位問題:
系統(tǒng)關(guān)于keyboard彈出監(jiān)聽有以下幾個通知(根據(jù)字面意思可以理解一下):
?UIKeyboardWillShowNotification
?UIKeyboardDidShowNotification
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification
UIKeyboardWillChangeFrameNotification //鍵盤Frame改變的通知
UIKeyboardDidChangeFrameNotification
UIKeyboardWillShowNotification,UIKeyboardWillHideNotification這個兩個通知一般搭配使用,但是在鍵盤切換或者第三方鍵盤接入的時候效果不是很好,所以推崇使用UIKeyboardWillChangeFrameNotification這個通知,可以檢測鍵盤彈出,收回,鍵盤切換或者第三方鍵盤如搜狗接入后鍵盤的實(shí)時變化

二:需要定位的視圖是否是當(dāng)前子視圖
?????? (1)如果是當(dāng)前子視圖只需要在鍵盤做出改變的時候監(jiān)聽即可:

主要是判斷鍵盤的坐標(biāo) 在所需要定位視圖的上方還是下方。
??? (2)如果當(dāng)前需要定位的視圖(按鈕或者評論行)不是當(dāng)前視圖的直接子視圖,比如這樣:

如果針對cell的label所在行進(jìn)行定位,所以需要把cell中的評論按鈕或者評論行轉(zhuǎn)換到當(dāng)前視圖或者window中:self.history_Y_offset = [cell.contentLabel convertRect:cell.contentLabel.bounds toView:window].origin.y;
轉(zhuǎn)換坐標(biāo)可以借鑒這篇文章:坐標(biāo)轉(zhuǎn)換
三:鍵盤切換或者第三方鍵盤接入問題處理:
if (self.tableview.isDragging) {
return;
}
NSDictionary *userInfo = notification.userInfo;
// 動畫的持續(xù)時間
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;//在設(shè)置完contentoffset后必須重新設(shè)置所需定位視圖的contentoffset,因?yàn)樵阪I盤切換過程中,該視圖的contentoffset會發(fā)生變化,切換到搜狗鍵盤也會發(fā)生變化;
demo 地址:評論或者回復(fù) 鍵盤定位問題?