Archive for April 9th, 2009

Code: Sliding UITextField

Having trouble with your UITextViews getting stuck under the keyboard? Here’s an elegant approach:

The iPhone’s onscreen keyboard occupies the bottom 216 pixels on screen (140 in landscape mode). That’s around half the screen, so if you ever have a text field you want to edit in the bottom half of the screen, it needs to move or it will get covered.

You can just animate the whole window upwards by the height of the keyboard when editing a text field in the bottom half but this doesn’t work well for text fields in the middle (they can get moved too far up).

Instead, I’m going to show you a method which divides the window as follows:

slidingsections

Everything in the top section will stay still when edited. Everything in the middle section will animate upwards by a fraction of the keyboard’s height (proportional to the field’s height within the middle section). Everything in the bottom section will animate upwards by the keyboard’s full height.

Much prettier than just jumping stuff around, indeed. And hey, it’s all about the pretty, isn’t it?

Continue Reading →
0