Resizing views around the keyboard

Jonah Williams ·

Most of my iPhone apps need to accept keyboard input somewhere and that means I need to reposition my UI elements such that the keyboard does not hide the field the user is typing in. The iPhone Application Programming Guide offers one example of Moving Content That Is Located Under The Keyboard but this is not necessarily the best solution.

Resizing a view’s frame

One option is certainly to implement Apple’s example code and resize the view’s frame based on the dimensions of the keyboard every time the keyboard is shown or hidden.

Resizing a view’s frame with a reference view

When working with complex views, especially views that support rotation, I have found it convenient to add an extra empty view to my xib files to serve as a reference. I set this reference view to the dimensions I want my resizable view to match when the keyboard is hidden. I can then resize my view relative to this constant frame and not need to worry about reversing my resize calculations when the keyboard is hidden.

Setting UIScrollView’s inset properties

When working with a scroll view (or table view) I’ve found that the best option is to set the view’s content and scroll inset properties. Since this does not change the scroll view’s frame it eliminates the flicker the view resizing as the keyboard is dismissed (notable if the scroll view’s parent view has a different background color).

Example code

Examples of all three techniques are available as an Xcode project at http://svn.carbonfive.com/public/jonah/ManagingKeyboardExample/