ios 11 beta 7 的 UIKeyboardWillShowNotification 问题

UIKeyboardWillShowNotification issues with ios 11 beta 7

在 iOS 11 beta 7 上测试我的应用程序 - 如果我的 UIViewController.

代码看起来像这样(从 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];
    }];
}

}

非常有趣 - 当我按下主页按钮(最小化应用程序)并重新打开它(而不是杀死它)时 - 布局是固定的。

这似乎是一个 iOS 11 beta 错误,但到目前为止我找不到任何参考资料。

很高兴知道是否有其他人遇到此问题。

使用 UIKeyboardFrameEndUserInfoKey 因为该键用于包含 CGRect 的 NSValue 对象,该 CGRect 在屏幕坐标中标识键盘的结束帧。 不要使用 UIKeyboardFrameBeginUserInfoKey

我也运行关注这个非运动问题。我怀疑开始和结束帧总是不正确,因为它们相同或几乎相同,但最近在 iOS 11 中得到修复,因此破坏了很多无法真正理解不同之处的代码在这两帧之间。

根据此处的信息

开始是键盘开始时的样子,这是大多数人不想要的。 End 是键盘外观的结果,大多数人只关心它。

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

滚动视图的零移动确实解决了我的问题。

如果您使用 UIKeyboardFrameBeginUserInfoKey 键获取键盘高度,请将其替换为 UIKeyboardFrameEndUserInfoKey 以获得正确的键盘高度。

在iOS11中,UIKeyboardFrameBeginUserInfoKey键的框架高度值为0,因为它是起始框架。要获得实际高度,我们需要结束帧,结束帧由 UIKeyboardFrameEndUserInfoKey.

返回

请参阅管理键盘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.