美文网首页
iOS根据键盘是否弹起,调整键盘上方inputview位置

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

作者: JasonEVA | 来源:发表于2016-03-07 09:53 被阅读922次

```
@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];

}];

}

```

相关文章

网友评论

      本文标题:iOS根据键盘是否弹起,调整键盘上方inputview位置

      本文链接:https://www.haomeiwen.com/subject/okiakttx.html