如何检测键盘类型的变化与新键盘类型的大小,类型,建议栏高度?

有没有办法检测键盘类型的变化与大小,类型和建议栏高度,从英语键盘更改为印地语,其中包含某种建议栏(见截图)。

普通英语键盘

在此处输入图像描述 在此处输入图像描述

第一个问题

改为印地语LIPI后 – 当我改变时,everthing很好,因为英语和印地语键盘的大小相同,但在开始输入印地语Lipi建议覆盖TextField

在此处输入图像描述 在此处输入图像描述

第二个问题

更改为表情符号 – 表情符号键盘高度与英语相比稍微多一点,所以再次键盘覆盖TextField

在此处输入图像描述

添加一个名为“UIKeyboardWillChangeFrameNotification”的通知的观察者。

通知的“userInfo”字典在屏幕上具有多个键盘框架。

添加一个观察者

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logNotification:) name:UIKeyboardWillChangeFrameNotification object:nil]; 

选择器与记录器

 - (void)logNotification:(NSNotification *)notification { NSLog(@"%@", notification); } 

userInfo字典内容

 userInfo = { UIKeyboardAnimationCurveUserInfoKey = 7; UIKeyboardAnimationDurationUserInfoKey = 0; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 441.5}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 315}, {320, 253}}"; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}"; UIKeyboardIsLocalUserInfoKey = 1; }} 

我也面临同样的问题。 我通过使用下面的代码解决了

 CGSize keyboardSize = [aNotification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 

使用UIKeyboardFrameEndUserInfoKey而不是使用UIKeyboardFrameBeginUserInfoKey。

斯威夫特4

注册观察员

 NotificationCenter.default.addObserver(self,selector:#selector(KeyboardWillChangeFrame),name:NSNotification.Name.UIKeyboardWillChangeFrame,object: nil) 

function

  @objc func KeyboardWillChangeFrame(_ notification: Notification){ if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue { let keyboardHeight = keyboardSize.height print("New Keyboard Height:",keyboardHeight) } } 

您需要按照屏幕上显示的方式获得键盘的高度

  • 发送UIKeyboardWillShowNotification时键盘高度为224

注册针对UIKeyboardWillShowNotificationUIKeyboardWillHideNotification通知的观察者:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 

设置键盘高度的全局变量:

 CGFloat _currentKeyboardHeight = 0.0f; 

实现keyboardWillShowkeyboardWillHide以对键盘高度的当前变化作出反应:

 - (void)keyboardWillShow:(NSNotification*)notification { NSDictionary *info = [notification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; CGFloat deltaHeight = kbSize.height - _currentKeyboardHeight; // Write code to adjust views accordingly using deltaHeight _currentKeyboardHeight = kbSize.height; } - (void)keyboardWillHide:(NSNotification*)notification { NSDictionary *info = [notification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; // Write code to adjust views accordingly using kbSize.height _currentKeyboardHeight = 0.0f; }