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来给当前控件添加约束。
相关文章
相关标签/搜索