Programmatically get height of keyboard in swift
Why we need to get height of keyboard in 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 height keyboard. Below are the codes that gives keyboard height in 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
You can remove observers in viewWillDisappear().