1

How to listen floating keyboard show and hide on iPad? UIKeyboardWillShowNotification or UIKeyboardWillHideNotification won't be called back. Main code below:

- (void)viewDidLoad() {
    [super viewDidLoad];

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

- (void)keyboardWillShow:(NSNotification *)note {
    // NOT called back
}

- (void)keyboardWillHide {
    // NOT called back
}
Smeegol
  • 2,014
  • 4
  • 29
  • 44

1 Answers1

0

The standard keyboard willShow and willHide do not work with a floating keyboard on iPad. Instead, we can observe the willChangeFrame notification, and look at the beginning and end frame to determine if the keyboard will appear or hide.

Here's an example in swift:

func viewDidLoad() {
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardFrameWillChange), name: NSNotification.Name(rawValue: UIResponder.keyboardWillChangeFrameNotification.rawValue), object: nil)
}

@objc func keyboardFrameWillChange() {
    let userInfo = notification.userInfo!
    let keyBoardEndRect = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
    let keyBoardBeginRect = userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as! CGRect

    if (keyBoardBeginRect.equalTo(CGRect.zero)) {
        // Floating keyboard will show        
    }

    if (keyBoardEndRect.equalTo(CGRect.zero)) {
        // Floating keyboard will hide        
    }
}
rohanphadte
  • 978
  • 6
  • 19