AUG
22
54230- Written by Pravin Dodia
- Published in Blog Post
- Be the first to comment!
Keyboard on iOS
The keyboard is present in nearly every application out there. Using the keyboard is the easiest way to provide users with a way to input alphanumeric data into applications. Trivial as it might look like in the beginning, a correct implementation of keyboard behavior can be a costly endeavor. Multiple keyboard states make it hard to implement logic that will behave correctly for all of them.
This post will cover my observations and experiences with the keyboard system in iOS. I will try to describe some basic concepts behind the keyboard notification system and take a closer look at the order in which notifications are sent.
Keyboard System
There are two main patterns across the Objective-C/Cocoa framework that give the user an idea of how the communication process between different objects functions – the delegation pattern and the notifications pattern.
The public keyboard API is built around the latter. You just inform the NSNotificationCenterobject that you want to receive some specific notifications. These notifications are later sent to you from somewhere else within the application when a specific type of event occurs.
Given that notifications are a generic pattern, there has to be a way to utilize them so they provide as much information as we want them to. Moreover, this information can be represented by a different number of objects which can, in turn, represent various types. For this particular purpose there is auserInfo property in the NSNotification class. This dictionary provides the elements listening for notifications with an additional context behind the triggering of the notification.
For keyboard notifications, we use the [NSNotificationCenter defaultCenter] method to get an instance of NSNotificationCenter class, which will be used for the notification’s registration process.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
And that’s all you need to start receiving notifications about the appearance of the keyboard. Well, what next?
Keyboard’s Notification
To put things bluntly, the iOS keyboard can be very annoying. Despite being really simple from the user’s perspective, in multiple cases the logic of the application suffers from either the lack or the misinterpretation of the system’s notifications. Below is a list of the keyboard’s notifications present in iOS:
- UIKeyboardWillShowNotification – notification sent when the keyboard is about to show.
- UIKeyboardDidShowNotification – notification sent when the keyboard is about to hide.
- UIKeyboardDidShowNotificationUIKeyboardWillHideNotification – notification sent when the keyboard has just been hidden.
- UIKeyboardWillChangeFrameNotification - notification sent when the kebyoard’s frame is about to change.
- UIKeyboardDidChangeFrameNotification – notification sent when the keyboard’s frame has just changed.
- UIKeyboardFrameBeginUserInfoKey – frame of the keyboard at the beginning of the current keyboard state change.
- UIKeyboardFrameEndUserInfoKey – frame of the keyboard at the end of the current keyboard state change.
- UIKeyboardAnimationDurationUserInfoKey – duration of the animation used to animate the change of the keyboard state.
- UIKeyboardAnimationCurveUserInfoKey – animation curve used to animate the change of the keyboard’s state.