iOS根据键盘是否弹起,调整键盘上方inputview位置

```
@property (nonatomic, strong) NSLayoutConstraint *bottomConstraint;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

- (void)keyboardWillChangeFrame:(NSNotification *)notification {

NSDictionary *keyboardInfo = notification.userInfo;

double animateDuration = [[keyboardInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

CGRect endFrame = [[keyboardInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

BOOL heightRefresh = CGRectGetHeight([UIScreen mainScreen].bounds) - CGRectGetMinY(endFrame) > 0;

if (!self.superview) {

return;

}

if (!self.bottomConstraint) {

for (NSLayoutConstraint *constraint in [self.superview constraints]) {

if (constraint.firstAttribute == NSLayoutAttributeBottom && constraint.firstItem == self) {

self.bottomConstraint = constraint;

break;

}

}

}

if (!self.bottomConstraint) {

return;

}

CGFloat heightOffset = 0;

if (heightRefresh) {

heightOffset = -CGRectGetHeight(endFrame);

}

self.bottomConstraint.constant = heightOffset;

[UIView animateWithDuration:animateDuration animations:^{

[self.superview layoutIfNeeded];

[self layoutIfNeeded];

}];

}

```

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容