要求
:
當鍵盤擋住輸入框時,輸入框自動向上彈到鍵盤上方。實現
:
這裏須要使用到Masonry的另一個方法mas_updateConstraints
。這個方法用於更新控件約束。
具體的實現方式能夠下載Demo來看,這裏只貼出鍵盤彈出時的處理代碼:動畫
- (void)keyboardWillChangeFrameNotification:(NSNotification *)notification { // 獲取鍵盤基本信息(動畫時長與鍵盤高度) NSDictionary *userInfo = [notification userInfo]; CGRect rect = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue]; CGFloat keyboardHeight = CGRectGetHeight(rect); CGFloat keyboardDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改下邊距約束 [_textField mas_updateConstraints:^(MASConstraintMaker *make) { make.bottom.mas_equalTo(-keyboardHeight); }]; // 更新約束 [UIView animateWithDuration:keyboardDuration animations:^{ [self.view layoutIfNeeded]; }]; }