Programmatically get height of keyboard in swift
Why we need to get keyboard height in iOS swift. Because with the introduction of different size of iPhone devices, getting keyboard height is a pain for iOS developers. And with the addition of suggestion toolbar above keyboard in iOS, it’s necessary for iOS developers to get exact height of the keyboard. So that developer’s can meet the app requirements where they have to show button above keyboard, when user is typing on text field or other input field.
iOS sends notifications each time keyboard comes up or goes off from screen. We, iOS developers can take advantage of this notification. Using keyboard show notification we can get the keyboard height. Below are the codes that gives keyboard height in iOS swift
Adding observers for UIKeyboardWillShow & UIKeyboardWillHide notifications
First we have to add notification observers to our class where we want to get keyboard height as soon as user start editing an input field. You can add them in viewWillAppear()
Adding selectors/methods definition for the notification observers added
Remove observers
You can remove observers in viewWillDisappear().