監(jiān)聽(tīng)鍵盤(pán)變化的通知。有改變的,顯示的,隱藏的幾個(gè)系統(tǒng)通知。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
這么處理,感覺(jué)跟隨鍵盤(pán)一起走比較自然一些。顯示和隱藏的通知記錄狀態(tài),改變的通知去改變尺寸做動(dòng)畫(huà),(暫時(shí)這么處理的,可能有更好的解決方案,還沒(méi)發(fā)現(xiàn))
#pragma mark *************** 鍵盤(pán)通知 ***************
-(void)keyboardWillChange:(NSNotification *)note {
NSDictionary *userInfo = note.userInfo;
CGRect keyboardFrame =[userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardY = keyboardFrame.origin.y;
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
if (duration == 0) {//切換輸入法
self.containerView.mj_y = keyboardY - SCREENHEIGHT;
}else{
[UIView animateWithDuration:duration delay:0.0 options:7 << 16 animations:^{
self.containerView.mj_y = keyboardY - SCREENHEIGHT;
if (self.keyboardView.alpha == 0) {
[self.chatTableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.keyboardView.mas_top).offset(-5);
}];
}else{
[self.chatTableView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.keyboardView.mas_top).offset(-BottomH-5+50);
}];
}
[self.containerView layoutIfNeeded];
} completion:nil];
}
}
-(void)keyboardWillShow:(NSNotification *)note {
self.keyboardView.alpha = 1;
}
- (void)keyboardWillHide:(NSNotification *)note {
self.keyboardView.alpha = 0;
}