How to detect keyboard type changes with the size, type, suggestion bar height of new keyboard type?

ⅰ亾dé卋堺 提交于 2019-12-05 13:27:44

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;
}}
QuocTV

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.

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)
     }
}

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!