问题
Is there any way to detect keyboard types changes with the size, type and suggestion bar height, change like from English keyboard to Hindi which contains some kind of suggestion bar (see screenshot).
Normal English Keyboard
First Problem
After changing to Hindi LIPI - When just I changed, everthing is fine because size of english and hindi keyboard is same, but after start typing Hindi Lipi suggestion cover the TextField
Second Problem
After changing to Emoji - Emoji keyboard hight little bit more as compare to english, So again keyboard cover the TextField
.
回答1:
Add an observer for a notification named "UIKeyboardWillChangeFrameNotification".
The "userInfo" dictionary for the notification has multiple frames of the keyboard on the screen.
adding an observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
selector with the logger
- (void)logNotification:(NSNotification *)notification {
NSLog(@"%@", notification);
}
userInfo dictionary content
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;
}}
回答2:
Same problem i too faced. And I resolved by using below code
CGSize keyboardSize = [aNotification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
Use UIKeyboardFrameEndUserInfoKey instead of using UIKeyboardFrameBeginUserInfoKey.
回答3:
Swift 4
Register Observer
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)
}
}
回答4:
You need to get the height of the Keyboard as per presented on the screen
- UIKeyboardWillShowNotification is sent with a keyboard height of 224
Register observers against the UIKeyboardWillShowNotification
and UIKeyboardWillHideNotification
notifications:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
Set a global variable for keyboard height:
CGFloat _currentKeyboardHeight = 0.0f;
Implement keyboardWillShow and keyboardWillHide to react to the current change in keyboard height:
- (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;
}
来源:https://stackoverflow.com/questions/35425758/how-to-detect-keyboard-type-changes-with-the-size-type-suggestion-bar-height-o