Moritz
Moritz

Reputation: 766

Keyboard height equals 0 when dismissed on drag

I am using collectionView?.keyboardDismissMode = .onDrag. The dismissal of the keyboard works fine. But if it is dismissed "on drag", the height printed equals 0 which causes problems later on. Can someone explain how I can get rid of this? In addition to that, I'd be very interested in the reasons as to why this is occurring.

@objc func keyboardWillHide(notification: Notification) {
    guard let userInfo = notification.userInfo as NSDictionary? else {
        return
    }

    guard let keyboardFrame = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as? NSValue else {
        return
    }

    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height

    print(keyboardHeight) //only prints 0 when dismissed on drag

    //do something with keyboard height
}

Upvotes: 0

Views: 66

Answers (1)

yakovlevvl
yakovlevvl

Reputation: 524

It is because you use UIKeyboardFrameEndUserInfoKey. UIKeyboardFrameEndUserInfoKey containing a CGRect that identifies the end frame of the keyboard in screen coordinates (in your case keyboard will hide, so height will be 0). So, you should use UIKeyboardFrameBeginUserInfoKey. It contains a CGRect that identifies the start frame of the keyboard in screen coordinates (before hiding).

Upvotes: 1

Related Questions