iOS8: What's going on with moving views during keyboard transitions?

徘徊边缘 提交于 2019-11-27 12:56:52

It's AutoLayout. Something changed in iOS8 and you can't just change frame or center points anymore if you have AutoLayout enabled. You have to create an outlet(s) of your constraint (vertical space) and update it accordingly instead of changing frame position. Constraints are like any other ui control and can have an outlet. Constraint change can be animated.

Example:

[UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0 options:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{        
    self.bottomSpaceConstraint.constant = adjustmentedValue;
    [self.view layoutIfNeeded];        
} completion:^(BOOL finished) {
}];

You should use UIKeyboardDidShowNotification (you're using will version) and everything will work as you expect:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardDidShow:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];

}

- (void)keyboardDidShow:(NSNotification *)notification
{
    // Called when the keyboard finished showing up
    [self nudgeUp];
}

The explanation is that with UIKeyboardWillShowNotification you are changing the frames too early. After your changes the system will relayout everything to accomodate the keyboard and your changes won't have any effect.

Also, I recommend you to switch to autolayout and forget about frames.

Try using the UIKeyboardWillShowNotification userInfo to give you the frame of the keyboard. Then move the onscreen elements based on that.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!