【问题标题】:UIKeyboardWillShowNotification returning incorrect frame for swift keyboardUIKeyboardWillShowNotification 为快速键盘返回不正确的帧
【发布时间】:2015-12-22 16:22:59
【问题描述】:

我正在使用UIKeyboardWillShowNotificationUIKeyboardWillHideNotification 来处理键盘。
这是显示键盘时调用的函数:

-(void) keyboardWillShow:(NSNotification *)note
{
    CGRect keyboardBounds;
    //[[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSValue* keyboardFrameBegin = [note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    keyboardBounds = [keyboardFrameBegin CGRectValue];  
}  

我得到 keyboardBounds = (0, 524, 320, 44) 并且我使用的是 5s。不知道为什么 origin.y 会出现 524(应该在 300 附近)和高度为 44 ! 我也尝试过注释行。两种方式的键盘绑定都是 44。此问题仅适用于快速键盘
this issue

【问题讨论】:

  • 看起来像一个竞争条件。使用UIKeyboardDidShowNotification时同样的问题?
  • 收听UIKeyboardDidChangeFrameNotification 并持续保存帧?
  • @FabioPoloni 我还没有注册 UIKeyboardDidChangeFrameNotification
  • 我仍然认为这是一个竞争条件,所以UIKeyboardDidChangeFrameNotification 可能会给您更多通知,但可能比其他通知更精确。
  • 这是在设备上还是模拟器上?您是否有连接到设备的硬件(可能是蓝牙)键盘,或者连接了模拟器的硬件键盘? (硬件 > 键盘 > 连接硬件键盘)

标签: ios iphone swift-keyboard


【解决方案1】:

问题出在快速键盘上,方法-(void) keyboardWillShow:(NSNotification *)note 被调用了三次,每次它返回三个不同的origin.y 值和高度,第一次给出keyboardBounds = (0, 524, 320, 44),第二次它给出keyboardBounds = (0, 308, 320, 260),最后第三次调用它返回keyboardBounds = (0, 271, 320, 297)。

由于第三方键盘大小不固定.. 它们的大小根据视图的布局方式固定,因此类似于自动布局案例(viewDidLoad,viewWillAppear viewWillLayout,viewDidLayout 然后 viewDidAppear,因此您可以了解确切的框架viewDidAppear 或 viewDidLayout,其中 View 已布局。),当它的视图完全布局时,这里得到精确的框架。

【讨论】:

  • UIKeyboard 没有打开的布局事件。
  • Swift键盘是自定义键盘,有事件,制作cmets前请仔细阅读问题
  • 如果您在iOS 11中遇到高度不正确的问题,请参考stackoverflow.com/a/46273070/2764966
【解决方案2】:

也许你可以试试其他的通知,比如:UIKeyboardDidShowNotificationUIKeyboardDidChangeFrameNotificationUIKeyboardWillShowNotification

只告诉你键盘会显示给你,不能告诉你确切的键盘显示帧。

【讨论】:

    猜你喜欢
    • 2015-01-04
    • 2015-10-19
    • 2016-06-14
    • 2011-10-12
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    相关资源
    最近更新 更多