如何通过新键盘类型的大小、类型、建议栏高度来检测键盘类型的变化?
How to detect keyboard type changes with the size, type, suggestion bar height of new keyboard type?
是否有任何方法可以检测键盘类型随大小、类型和建议栏高度的变化,例如从英文键盘更改为包含某种建议栏的印地语(见屏幕截图)。
普通英文键盘
First Problem
更改为印地语 LIPI 后 - 当我刚刚更改时,一切都很好,因为英语和印地语键盘的大小相同,但在开始输入印地语 Lipi 后建议覆盖 TextField
Second Problem
更改为 Emoji 后 - Emoji 键盘比英文键盘高一点,所以键盘再次覆盖 TextField
。
您需要获取屏幕上显示的键盘高度
- 发送 UIKeyboardWillShowNotification 时键盘高度为 224
针对 UIKeyboardWillShowNotification
和 UIKeyboardWillHideNotification
通知注册观察员:
[[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;
实施 keyboardWillShow 和 keyboardWillHide 以响应当前键盘高度的变化:
- (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;
}
为名为 "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。
Swift 4
注册观察员
NotificationCenter.default.addObserver(self,selector:#selector(KeyboardWillChangeFrame),name:NSNotification.Name.UIKeyboardWillChangeFrame,object: nil)
函数
@objc func KeyboardWillChangeFrame(_ notification: Notification){
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardSize.height
print("New Keyboard Height:",keyboardHeight)
}
}
是否有任何方法可以检测键盘类型随大小、类型和建议栏高度的变化,例如从英文键盘更改为包含某种建议栏的印地语(见屏幕截图)。
普通英文键盘
First Problem
更改为印地语 LIPI 后 - 当我刚刚更改时,一切都很好,因为英语和印地语键盘的大小相同,但在开始输入印地语 Lipi 后建议覆盖 TextField
Second Problem
更改为 Emoji 后 - Emoji 键盘比英文键盘高一点,所以键盘再次覆盖 TextField
。
您需要获取屏幕上显示的键盘高度
- 发送 UIKeyboardWillShowNotification 时键盘高度为 224
针对 UIKeyboardWillShowNotification
和 UIKeyboardWillHideNotification
通知注册观察员:
[[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;
实施 keyboardWillShow 和 keyboardWillHide 以响应当前键盘高度的变化:
- (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;
}
为名为 "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。
Swift 4
注册观察员
NotificationCenter.default.addObserver(self,selector:#selector(KeyboardWillChangeFrame),name:NSNotification.Name.UIKeyboardWillChangeFrame,object: nil)
函数
@objc func KeyboardWillChangeFrame(_ notification: Notification){
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardSize.height
print("New Keyboard Height:",keyboardHeight)
}
}