自定義鍵盤(pán)上方工具欄隨鍵盤(pán)一起運(yùn)動(dòng)的問(wèn)題

監(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;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容