使用ios 11 beta 7的UIKeyboardWillShowNotification问题

在iOS 11 beta 7上testing我的应用程序 – 如果我的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]; }]; } 

}

有趣的是 – 当我按主页button(最小化应用程序),并重新打开它(不杀死它) – 布局是固定的。

这看起来像一个iOS 11的testing版的bug,但到目前为止我还找不到任何的参考。

很高兴知道别人是否有这个问题。

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

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

在iOS 11中, UIKeyboardFrameBeginUserInfoKey关键帧的高度值是0,因为它是一个开始帧。 为了得到实际的高度,我们需要结束帧,结束帧由UIKeyboardFrameEndUserInfoKey返回。

请参阅pipe理键盘文档 :

UIKeyboardFrameBeginUserInfoKey包含CGRect的NSValue对象的关键字,用于标识屏幕坐标中键盘的起始帧 。 由于界面方向的改变,这些坐标不考虑应用于窗口内容的任何旋转因子。 因此,在使用它之前,可能需要将矩形转换为窗口坐标(使用convertRect:fromWindow:方法)或查看坐标(使用convertRect:fromView:方法)。

UIKeyboardFrameEndUserInfoKey包含CGRect的NSValue对象的关键字,用于标识屏幕坐标中键盘的结束帧 。 由于界面方向的改变,这些坐标不考虑应用于窗口内容的任何旋转因子。 因此,在使用它之前,可能需要将矩形转换为窗口坐标(使用convertRect:fromWindow:方法)或查看坐标(使用convertRect:fromView:方法)。

我遇到了这个不运动的问题。 我怀疑开始和结束帧总是不正确的,因为它们是相同的或几乎相同的,但是最近在iOS 11中被修复,并因此破坏了很多代码,但是没有真正理解这两个帧之间的差异。

根据这里的信息https://stackoverflow.com/a/14326257/5282052

开始是键盘开始的样子,这是大多数人不会想要的。 最终是键盘看起来像什么,这是最关心的结果。

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

确实通过滚动视图的ZERO移动来解决我的问题。