問題描述
通過修改自動布局控件NSLayoutConstraint的constant屬性可以實現(xiàn)控制位置的修改诱贿,但是測試發(fā)現(xiàn)UIView提供動畫的代碼沒有起作用
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);
解決方式
需要在目標(biāo)動畫的控件上添加layoutIfNeeded函數(shù)的調(diào)用
- (void) keyboardInputTextFieldWillShow:(NSNotification *) notification {
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
double animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
_keyboardAnimationDuration = animationDuration;
[UIView animateWithDuration:animationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.topConstraintHeight.constant = CGRectGetHeight(self.view.frame) - CGRectGetHeight(_inputView.frame) - kbSize.height;
[self.inputView layoutIfNeeded]; // add
} completion:^(BOOL finished) {
}];
}