从通知获取键盘高度

Getting keyboard height from Notification

我正在使用代码从通知获取键盘高度

  CGFloat height = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].height;

我用它来呈现其他一些视图,它的高度与键盘的高度相同。 为此,我创建了临时文本字段并从中获取键盘高度。我使用代码:

 UITextField *tempTextField = [[UITextField alloc]init];
[[[[UIApplication sharedApplication] windows] lastObject] addSubview:tempTextField];
[tempTextField becomeFirstResponder];
[tempTextField resignFirstResponder];
[tempTextField removeFromSuperview];

然后我得到身高值。它等于 253。但后来我从真实文本字段中获取高度为 216。我需要在从临时文本字段发送的通知中获取 216 值。我怎样才能得到它?

216 是没有 QuickType 的 iOS 内置键盘的高度。 253在里面。

因此在您的情况下,您必须将 autocorrectionType 设置为 no。

UITextField *tempTextField = [[UITextField alloc]init];
tempTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[[[[UIApplication sharedApplication] windows] lastObject] addSubview:tempTextField];
[tempTextField becomeFirstResponder];
[tempTextField resignFirstResponder];
[tempTextField removeFromSuperview];

我不知道你想用高度做什么,但我不能确定这是获得键盘高度的最佳方法。