iOS 11 beta 7中UIKeyboardWillShowNotification的问题

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测试版的一个bug,但我目前还没有找到任何相关参考。

如果其他人也遇到了这个问题,我很乐意知道。


2
UIKeyboardFrameBeginUserInfoKey 替换为 UIKeyboardframeenduserinfokey - Aditya Srivastava
看起来运行正常!非常感谢@AdityaSrivastava! - Gil
很高兴能帮助你。 - Aditya Srivastava
为什么这个通知把键盘高度给出为0.0? 以下是我获取键盘大小的方式...let info1: NSDictionary = (notification as NSNotification).userInfo! as NSDictionary let value: NSValue = info1.value(forKey: UIKeyboardFrameBeginUserInfoKey) as! NSValue - Suryakant Sharma
但它可以使用UIKeyboardframeenduserinfokey吗?@AdityaSrivastava 有什么想法吗? - Suryakant Sharma
谢谢,它起作用了! - Thein
3个回答

12

使用 UIKeyboardFrameEndUserInfoKey,因为它是包含一个 CGRect 的 NSValue 对象,该 CGRect 标识了键盘在屏幕坐标系中的结束位置。不要使用 UIKeyboardFrameBeginUserInfoKey


4
如果您正在使用 UIKeyboardFrameBeginUserInfoKey 键来获取键盘高度,请将其替换为 UIKeyboardFrameEndUserInfoKey 以获取正确的键盘高度。
在 iOS 11 中,UIKeyboardFrameBeginUserInfoKey 键的框架高度值为 0,因为它是起始框架。要获取实际高度,我们需要结束框架,而结束框架由 UIKeyboardFrameEndUserInfoKey 返回。
请参阅 Managing the Keyboard 文档:(文档链接)

UIKeyboardFrameBeginUserInfoKey 键用于包含 CGRect 的 NSValue 对象,该对象标识屏幕坐标中键盘的起始框架。这些坐标不考虑任何旋转因素,这些因素是由于接口方向更改而应用于窗口内容的。因此,在使用之前,您可能需要将矩形转换为窗口坐标(使用 convertRect:fromWindow: 方法)或视图坐标(使用 convertRect:fromView: 方法)。

UIKeyboardFrameEndUserInfoKey 键用于包含 CGRect 的 NSValue 对象,该对象标识屏幕坐标中键盘的结束框架。这些坐标不考虑任何旋转因素,这些因素是由于接口方向更改而应用于窗口内容的。因此,在使用之前,您可能需要将矩形转换为窗口坐标(使用 convertRect:fromWindow: 方法)或视图坐标(使用 convertRect:fromView: 方法)。


1
我也遇到了这个不动问题。我怀疑开始和结束帧一直是错误的,因为它们相同或接近相同,但最近在iOS 11中被修复,这样就破坏了许多代码,这些代码并不真正理解这两个帧之间的差异。
根据这里的信息 https://stackoverflow.com/a/14326257/5282052 开始是键盘开始时的外观,大多数人都不希望看到。 结束是键盘将要呈现的结果,这是大多数人关心的。
[[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
确实解决了我的滚动视图零移动问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接