UIKeyboardWillShowNotification issues with ios 11 beta 7

2.2k views Asked by At

Testing my app on iOS 11 beta 7 - it seems like the keyboard doesn't push up the content if my UIViewController.

The code looks like this (working since iOS7):

- (void)handleNotification:(NSNotification*)notification {
if (notification.name == UIKeyboardWillShowNotification) {
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    nextButtonALBottomDistance.constant = keyboardSize.height + initialPhoneBottomDistance;
    codeBottomViewALBottomDistance.constant = keyboardSize.height + initialCodeBottomDistance;
    double animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:animationDuration animations:^{
        [self.view layoutIfNeeded];
    }];
}
else if (notification.name == UIKeyboardWillHideNotification) {
    nextButtonALBottomDistance.constant = initialPhoneBottomDistance;
    codeBottomViewALBottomDistance.constant = initialCodeBottomDistance;
    double animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:animationDuration animations:^{
        [self.view layoutIfNeeded];
    }];
}

}

Interesting enough - when I press the home button (minimizing the app) and reopening it (without killing it) - the layout is fixed.

It seems like an iOS 11 beta bug, but I couldn't find any reference to it so far.

Happy to know if someone else is having this issue.

3

There are 3 answers

0
Nirav Kotecha On BEST ANSWER

Use UIKeyboardFrameEndUserInfoKey because that key is for the NSValue object containing a CGRect that identifies an end frame of the keyboard in screen coordinates. Do not use UIKeyboardFrameBeginUserInfoKey.

0
Craig On

I ran into this non-movement issue as well. I suspect that the beginning & end frame were always incorrect, in that they were the same or nearly the same, but was fixed recently in iOS 11 and as such broke a lot of code that didn't truly understand the different between these two frames.

Based on the information here https://stackoverflow.com/a/14326257/5282052

Begin is what the keyboard starts off looking like, which most won't want. End is the result of what the keyboard will look like, which most only are concerned with.

[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

Does indeed resolve my issue with a ZERO movement of the scrollview.

0
ViruMax On

If you are using UIKeyboardFrameBeginUserInfoKey key for getting keyboard height, replace it with UIKeyboardFrameEndUserInfoKey to get the correct keyboard height.

In iOS 11, height value of the frame for UIKeyboardFrameBeginUserInfoKey key is 0 because it is a start frame. To get the actual height we need the end frame, and end frame is returned by UIKeyboardFrameEndUserInfoKey.

Please refer Managing the Keyboard documentation:

UIKeyboardFrameBeginUserInfoKey The key for an NSValue object containing a CGRect that identifies the start frame of the keyboard in screen coordinates. These coordinates do not take into account any rotation factors applied to the window’s contents as a result of interface orientation changes. Thus, you may need to convert the rectangle to window coordinates (using the convertRect:fromWindow: method) or to view coordinates (using the convertRect:fromView: method) before using it.

UIKeyboardFrameEndUserInfoKey The key for an NSValue object containing a CGRect that identifies the end frame of the keyboard in screen coordinates. These coordinates do not take into account any rotation factors applied to the window’s contents as a result of interface orientation changes. Thus, you may need to convert the rectangle to window coordinates (using the convertRect:fromWindow: method) or to view coordinates (using the convertRect:fromView: method) before using it.