iOS 11 - 键盘高度在键盘通知中返回0

Has*_*ssy 81 objective-c nsnotificationcenter ios

我一直在使用键盘通知没有任何问题,并获得键盘的准确高度.

- (void)keyboardDidShow:(NSNotification *) notification{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    NSLog(@"%f",keyboardSize.height);}
Run Code Online (Sandbox Code Playgroud)

但是在iOS 11中,调用通知时键盘的大小为0.

这种情况下出现的问题是什么?我正在使用xcode 9 Beta 5

s.z*_*een 183

用这个:

CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
Run Code Online (Sandbox Code Playgroud)

对于Swift,您可以使用:

let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
Run Code Online (Sandbox Code Playgroud)

  • 从UIKeyboardFrameBeginUserInfoKey更改为UIKeyboardFrameEndUserInfoKey就可以了. (5认同)
  • 合作!! 但怎么样?对此有任何解释. (4认同)

Adi*_*ava 100

更换 UIKeyboardFrameBeginUserInfoKey

UIKeyboardFrameEndUserInfoKey

以下内容来自Apple Docs.

UIKeyboardFrameBeginUserInfoKey-包含CGRect的NSValue对象的键,该CGRect用于标识屏幕坐标中键盘的起始帧.

UIKeyboardFrameEndUserInfoKey - 包含CGRect的NSValue对象的键,该CGRect在屏幕坐标中标识键盘的结束帧.

  • Apple需要更新他们的示例代码,因为它在iOS 11上不再起作用.同样来自Apple docs,他们使用UIKeyboardFrameBeginUserInfoKey:https://developer.apple.com/library/content/documentation/StringsTextFonts/Conceptual/ TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html (13认同)

小智 7

试试这个:

替换UIKeyboardFrameBeginUserInfoKeyUIKeyboardFrameEndUserInfoKey


and*_*dym 5

我有一个使用Xcode版本9.0(9A235)的类似问题; 虽然我使用的是Swift.在我的keyboardWillShow方法中,我写了以下内容:

if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {

    let heightValue = keyboardSize.height    

    ...
}
Run Code Online (Sandbox Code Playgroud)

奇怪的是,第一次调用keyboardWillShow时,heightValue为216.0,但在后续调用中它变为0!也许这是一个Xcode错误.

我用UIKeyboardFrameEndUserInfoKey替换了UIKeyboardFrameBeginUserInfoKey,它为我解决了这个问题.


Suj*_*U N 5

此问题在iOS 11上发生。

更换

“ UIKeyboardFrameBeginUserInfoKey”和“ UIKeyboardFrameEndUserInfoKey”

如下所示将解决问题

Objective-C代码:

CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
Run Code Online (Sandbox Code Playgroud)

Swift 2.3:

let keyboardSize = (NfnPsgVar.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue().size
Run Code Online (Sandbox Code Playgroud)

迅捷3:

let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
Run Code Online (Sandbox Code Playgroud)