Chris Hansen
Chris Hansen

Reputation: 8635

Keep textfield above keyboard

I am trying to figure out how to keep the textfield about the keyboard in IOS. I tried the different code on stackoverflow but none works perfectly. Like if the textfield is above the keyboard and I click on the field, it does not move up. How can I show the textfield above the keyboard at all times? thanks!

Upvotes: 3

Views: 1927

Answers (1)

Ephemera
Ephemera

Reputation: 8960

lakesh's link is an excellent resource to look at.

There are essentially two ways to handle the situation.

Method 1:

You can encapsulate your view inside a UIScrollView and when the keyboard pops up, you should scroll your entire view up an equal distance to account for the space taken up by the keyboard.

Method 2:

Take the ultimate parent UIView inside your current UIViewController and change its frame (ideally with an animation) so that it moves off the top of the screen and makes way for the keyboard.

As a general guide, Method 1 is the preferred method. This is because you can still access the UI 'higher up' in the UIScrollView by scrolling up to it (consider say, a form with multiple fields). In Method 2 the user cannot return to the other elements in the view without the keyboard first being dismissed. Of course, it may be that you don't need to see the rest of the view while accepting keyboard input, but that decision is up to you.

Upvotes: 1

Related Questions