20

I used :

NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)

@objc func keyboardWillShow(notification: NSNotification) {
      if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
      let keyboardHeight : Int = Int(keyboardSize.height)
      print("keyboardHeight",keyboardHeight)
      KeyboardHeightVar = keyboardHeight
      }
}

to change to get the height of the keyboard, but the height doesn't include the suggestions bar. How do I get the value of the keyboard height plus the suggestions bar height?

Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50
Saved Files
  • 217
  • 1
  • 2
  • 4
  • 1
    Possible duplicate of [How to get height of Keyboard?](https://stackoverflow.com/questions/31774006/how-to-get-height-of-keyboard) – Iraniya Naynesh Jun 08 '18 at 11:55

4 Answers4

9

Using the UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey returns the correct keyboard height. For example, if the keyboard without the toolbar, it returns 216.0 height. With the toolbar - 260.0

IvanPavliuk
  • 1,460
  • 1
  • 21
  • 16
  • it gets called twice for the first time and for rest of the time it is called once. Any reason? – chetan panchal May 24 '19 at 15:46
  • Hmm, I don't have this problem. In the `viewDidLoad` you can do this: `NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)` – IvanPavliuk May 26 '19 at 17:10
  • is thier anyway keyboardWillshow method gets called once? – chetan panchal May 27 '19 at 07:35
  • 2
    Not sure if there's been a change in something, but on mine I get the exact same value for height using both methods, even though there is definitely a suggestion bar present. – NickGlowsinDark Oct 25 '19 at 22:56
  • For Swift4 4/5 I use this and it works: let userInfo = notification.userInfo ?? [:] let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue let height = keyboardFrame.height – clopex Oct 12 '20 at 08:52
  • I also get the exact same values for both, with and without toolbar – trusk Aug 13 '21 at 11:47
5

Use UIKeyboardFrameEndUserInfoKey instead of UIKeyboardFrameBeginUserInfoKey and UIKeyboardDidShow instead of UIKeyboardWillShow.

NotificationCenter.default.addObserver(self, selector: 

#selector(keyboardWillShow), name: .UIKeyboardDidShow, object: nil)
    @objc func keyboardWillShow(notification: NSNotification) {

            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
                let keyboardHeight : Int = Int(keyboardSize.height)
                print("keyboardHeight",keyboardHeight)
                KeyboardHeightVar = keyboardHeight
            }

        }
Enrique Bermúdez
  • 1,740
  • 2
  • 11
  • 25
  • 12
    that method only gets the height of the keyboard. I am trying to get the height including the suggestions bar – Saved Files Jun 08 '18 at 12:07
4

Try using UIKeyboardDidShow instead.

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)

You'll get the callback in keyboardWasShown method whenever the keyboard is appear on the screen,

@objc func keyboardWasShown(_ notification : Notification)
{
    let info = (notification as NSNotification).userInfo
    let value = info?[UIKeyboardFrameEndUserInfoKey]
    if let rawFrame = (value as AnyObject).cgRectValue
    {
        let keyboardFrame = self.reportItTableView.convert(rawFrame, from: nil)
        let keyboardHeight = keyboardFrame.height //Height of the keyboard
    }
}
PGDev
  • 23,751
  • 6
  • 34
  • 88
  • 2
    In that case, we lose the smooth animation! – IvanPavliuk May 04 '19 at 09:46
  • This answer helped me, I am using the keyboard frame did change notification and retrieving the frame end user info value, but was not converting the keyboard frame to the enclosing view, once I did that then I get the correct result and can scroll the view correctly. – ptc Apr 22 '20 at 23:07
  • this method gets called 3 times giving me 3 different hights. – Parth Jan 29 '21 at 13:20
2

First you need to register for notification that triggered when keyboard will be visible.

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)

Get keyboard height in method...

@objc func keyboardWillShow(_ notification: Notification) {

 if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height
 }
}  
Mahendra
  • 8,448
  • 3
  • 33
  • 56