How to get height of Keyboard? How to get height of Keyboard? ios ios

How to get height of Keyboard?


Swift

You can get the keyboard height by subscribing to the UIKeyboardWillShowNotification notification. (Assuming you want to know what the height will be before it's shown).

Swift 4

NotificationCenter.default.addObserver(    self,    selector: #selector(keyboardWillShow),    name: UIResponder.keyboardWillShowNotification,    object: nil)
@objc func keyboardWillShow(_ notification: Notification) {    if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {        let keyboardRectangle = keyboardFrame.cgRectValue        let keyboardHeight = keyboardRectangle.height    }}

Swift 3

NotificationCenter.default.addObserver(    self,    selector: #selector(keyboardWillShow),    name: NSNotification.Name.UIKeyboardWillShow,    object: nil)
@objc func keyboardWillShow(_ notification: Notification) {    if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {        let keyboardRectangle = keyboardFrame.cgRectValue        let keyboardHeight = keyboardRectangle.height    }}

Swift 2

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
func keyboardWillShow(notification: NSNotification) {        let userInfo: NSDictionary = notification.userInfo!        let keyboardFrame: NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue        let keyboardRectangle = keyboardFrame.CGRectValue()        let keyboardHeight = keyboardRectangle.height    }


Swift 3.0 and Swift 4.1

1- Register the notification in the viewWillAppear method:

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

2- Method to be called:

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


Swift 4 and Constraints

To your tableview add a bottom constraint relative to the bottom safe area. In my case the constraint is called tableViewBottomLayoutConstraint.

@IBOutlet weak var tableViewBottomLayoutConstraint: NSLayoutConstraint!override func viewWillAppear(_ animated: Bool) {    super.viewWillAppear(animated)    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(notification:)), name: .UIKeyboardWillShow, object: nil)    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear(notification:)), name: .UIKeyboardWillHide, object: nil)}override func viewWillDisappear(_ animated: Bool) {    super.viewWillDisappear(animated)    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow , object: nil)    NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide , object: nil)}@objc func keyboardWillAppear(notification: NSNotification?) {    guard let keyboardFrame = notification?.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else {        return    }    let keyboardHeight: CGFloat    if #available(iOS 11.0, *) {        keyboardHeight = keyboardFrame.cgRectValue.height - self.view.safeAreaInsets.bottom    } else {        keyboardHeight = keyboardFrame.cgRectValue.height    }    tableViewBottomLayoutConstraint.constant = keyboardHeight}@objc func keyboardWillDisappear(notification: NSNotification?) {    tableViewBottomLayoutConstraint.constant = 0.0}