【问题标题】:UIKeyboardWillShowNotification issues with ios 11 beta 7ios 11 beta 7 的 UIKeyboardWillShowNotification 问题
【发布时间】:2018-02-05 20:09:00
【问题描述】:

在 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 测试版错误,但到目前为止我找不到任何参考。

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

【问题讨论】:

  • UIKeyboardframeenduserinfokey替换UIKeyboardFrameBeginUserInfoKey
  • 似乎有效!非常感谢@AdityaSrivastava!
  • 很高兴为您提供帮助
  • 为什么这个通知将键盘高度设为 0.0 这就是我获取键盘大小的方式...let info1: NSDictionary = (notification as NSNotification).userInfo! as NSDictionary let value: NSValue = info1.value(forKey: UIKeyboardFrameBeginUserInfoKey) as! NSValue
  • 但它适用于 UIKeyboardframeenduserinfokey @AdityaSrivastava 任何想法?

标签: ios objective-c keyboard ios11


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    请参考管理键盘documentation:

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

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

    【讨论】:

      【解决方案3】:

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

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

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

      [[[通知用户信息] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-14
        • 1970-01-01
        • 2020-01-01
        • 1970-01-01
        相关资源
        最近更新 更多