Masonry約束動畫 以及 鍵盤彈起

要求
當鍵盤擋住輸入框時,輸入框自動向上彈到鍵盤上方。
實現
這裏須要使用到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]; }]; }

總結:

  1. 能夠給控件添加left/right/top/bottom/size/height/width/insert約束;
  2. 庫提供了三個方法,mas_makeConstraints添加約束,mas_updateConstraints修改約束,mas_remakeConstraints清除之前約束並添加新約束;
  3. 能夠經過view.mas_bottom得到view的某個約束;
  4. 在約束的block中,使用make來給當前控件添加約束。
相關文章
相關標籤/搜索