3

I want to get the keyboard height of the device to build some objects at the same height. These objects will be there before the keyboard actually appears so I can't use a method like this. I was wondering if there's a way to get the height of the keyboard without it actually appearing?

Community
  • 1
  • 1
  • You have to use the linked approach because you can't know what the keyboard size will be until it is about to be shown. The user might have custom keyboards installed or might switch keyboards. Some keyboard will have different heights and some may have toolbars on them. – rmaddy Nov 07 '16 at 21:24
  • I wrote an article about this topic, if you still need help check it out. https://federicabenacquista.medium.com/list-of-the-official-ios-keyboards-heights-and-how-to-calculate-them-c2b844ef54b9?sk=e7490c5f0236be791a1b6f4eda6e1e2f – Federica Benacquista Jan 06 '21 at 00:48

2 Answers2

0

Iv also had a similar issue. I havent found a way to get the keyboard height without the keyboard showing up first, but I do have a way around it.

var keyboardHeight = CGFloat()
var keyboardFirstTime = true

in view did load

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

separate function that gives keyboard height

func keyboardWillShow(_ notification:Notification)
{
    let userInfo:NSDictionary = (notification as NSNotification).userInfo! as NSDictionary
    let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.cgRectValue
    keyboardHeight = keyboardRectangle.height
}

in textFieldDidBeginEditing

func textFieldDidBeginEditing(_ textField: UITextField)
{
    //adds .01 delay to wait and assign keyboard height for the first time
    if keyboardFirstTime == true
    {
        let when = DispatchTime.now() + 0.01
        DispatchQueue.main.asyncAfter(deadline: when)
        {
            UIView.animate(withDuration: 0.25, delay: 0, options: [.curveEaseOut], animations:
                {
                self.searchField.frame.origin.y = self.view.frame.height - self.keyboardHeight - self.searchField.frame.size.height
                }, completion: nil)
        }
        keyboardFirstTime = false
    }
    else
    {
        UIView.animate(withDuration: 0.3, delay: 0, options: [.curveEaseOut], animations:
            {
                self.searchField.frame.origin.y = self.view.frame.height - self.keyboardHeight - self.searchField.frame.size.height
            }, completion: nil)
    }

    textField.text = ""
}
Andrew Cook
  • 116
  • 1
  • 8
  • the keyboardFirstTime boolean adds a very small delay. This gives the code enough time to calculate what the keyboard height will be and set it to a var. – Andrew Cook Nov 07 '16 at 21:18
0

One hack is to create a textField somewhere and in viewDidLoad make that textField first responder and then hide it instantly. This will only happen once when viewLoads.

Simran Singh
  • 445
  • 6
  • 8