DETECTING THE PRESENCE OF THE KEYBOARD

Up to this point, you have seen the various ways to hide the keyboard after you are done using it. However, note one problem: When the keyboard appears, it takes up a significant portion of the screen. If your Text Field is located at the bottom of the screen, it would be covered by the keyboard. As a programmer, it is your duty to ensure that the view is relocated to a visible portion of the screen. Surprisingly, this is not taken care of by the SDK; you have to do the hard work yourself.

image NOTE The keyboard in the iPhone (3G and 3GS) takes up 216 pixels (432 pixels for iPhone 4 and iPhone 4S) in height when in portrait mode, and 162 pixels (324 pixels for iPhone 4 and iPhone 4S) when in landscape mode. For the iPad, the keyboard takes up 264 pixels in height when in portrait mode, and 352 pixels when in landscape mode.

First, though, it is important that you understand a few key concepts related to the keyboard:

  • You need to be able to programmatically know when a keyboard is visible or hidden. To do so, your application needs to register for the UIKeyboardDidShowNotification and UIKeyboardDidHideNotification notifications.
  • You also need to know when and which Text Field is currently being edited so that you can relocate it to a visible portion of the screen. You can determine this information through the textFieldDidBeginEditing: method ...

Get Beginning iOS 5 Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.