View aligned to top of keyboard appears in wrong place in iOS 9 Shortcut Bar only mode

后端 未结 1 1415
闹比i
闹比i 2021-01-06 10:57

iOS 9 adds a Shortcut Bar to the iOS 8 QuickType bar.

As part of this change, if you connect a bluetooth keyboard to an iPad, the keyboard is in a minimized Shortcut

相关标签:
1条回答
  • 2021-01-06 11:46

    The problem is that most code out there (including Apple's) ignores the fact that UIKeyboardFrameEndUserInfoKey is a CGRect and not a CGSize.

    // ❌ Bad code, do not use
    - (void)keyboardWasShown:(NSNotification*)aNotification {
        NSDictionary* info = [aNotification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        CGRect bkgndRect = activeField.superview.frame;
        bkgndRect.size.height += kbSize.height;
        [activeField.superview setFrame:bkgndRect];
        [scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height) animated:YES];
    }
    

    Here you see that only the keyboard height (kbSize.height) is being used. The origin of the rect is important, and should not be ignored.

    When the keyboard is visible, this is the rect that is reported:

    When the keyboard is in Shortcut Bar only mode, this is the rect:

    Notice how the majority of the keyboard is offscreen, yet it is still the same height.

    To get the correct behavior, use CGRectIntersection with the view's bounds and the keyboard frame within that view:

    // ✅ Good code, use
    CGRect keyboardScreenEndFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect keyboardViewEndFrame = [self.view convertRect:keyboardScreenEndFrame fromView:self.view.window];
    CGRect keyboardFrame = CGRectIntersection(self.view.bounds, keyboardViewEndFrame);
    CGFloat keyboardHeight = keyboardFrame.size.height; // = 55
    

    For this same reason, UIKeyboardFrameEndUserInfoKey should be used as opposed to UIKeyboardFrameBeginUserInfoKey.

    0 讨论(0)
提交回复
热议问题