0

I am working on a keyboard extension app and I need the height of the default keyboard.

To get height of the KeyBoard, I have tried dozens of solutions like this, but the Selector function never gets called. Is there another way to find the height of default keyboard?

S.S.D
  • 1,579
  • 2
  • 12
  • 23

2 Answers2

0

This code will show the height of your keyboard -

func showKeyboard(notification: NSNotification) {
            if let sizeOFkeyboard = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
                let keyboardHeight = keyboardSize.height
            }
    }
Rashed
  • 2,349
  • 11
  • 26
0

This is working perfectly for me in Swift 4.1:

var keyboardHeight: CGFloat = 0.0

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(true)

        // Register Notification, To know When Key Board Appear.
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: Notification.Name.UIKeyboardWillShow, object: nil)

        // Register Notification, To know When Key Board Hides.
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(true)

        // De register the notifications
        NotificationCenter.default.removeObserver(self)
    }

// MARK: - Keyboard Events

    @objc func keyboardWillShow(_ notification: Notification) {
        if let keyboardFrame: NSValue = notification.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue {
            let keyboardRectangle = keyboardFrame.cgRectValue
            keyboardHeight = keyboardRectangle.height
        }
// use keyboard duration if needed (to animate your view)
        let keyboardDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double

    }

    @objc func keyboardWillHide(_ notification: Notification) {

        let keyboardDuration = notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? Double

    }