In this short tutorial I will be showing you how to ’scroll’ a UITextField into view when editing begins, so it is not hidden behind the keyboard. First off I will guess that you have already implemented a UITextField into your application, and all you are looking to know how to do is scroll it upwards.
So first off begin by implementing the UITextField into your application. Don’t forget to do all the linking, and set the delegate to the .m file the TextField is in.
Now flip to the .m file in Xcode that the TextField is in. Begin typing the following code:

Description of each line of code:
13: Called when a text field has been touched to be edited (Begun editing).
14: Comment.
15: Create a new animation.
16: Set the animation delegate to equal self.
17: Set the duration of the animation to 0.5 seconds. You can change this value to change the speed of the animation if you wish.
18: Set the type of animation; how it reacts, and how the UITextField will move.
19-22: Set the frame of where the UITextField will be when you begin editing it. In this case, it moves up 100 (Subtract 100 from it’s y origin).
23: Commit the animations. This tells the program to begin the animations and start moving the UITextField.
24: Return YES is the answer to the BOOL textFieldShouldBeginEditing.
25: Close the BOOL.
Next, in the same .m file, type the following code:

Description of each line of code:
27: Called when the user presses the return key on the keyboard (End/finished editing).
28: Resign the first responder, which in this case is the keyboard.
29: Create a new animation.
30: Set the animation delegate to equal self.
31: Set the duration of the animation to 0.5 seconds. You can change this value to change the speed of the animation if you wish.
32: Set the type of animation; how it reacts, and how the UITextField will move.
33-37: Set the frame to where the UITextField was before you begun editing it. In this case, it moves back down 100 (Add 100 to it’s y origin).
38: Commit the animations. This tells the program to begin the animations and start moving the UITextField.
39: Return YES is the answer to the BOOL textFieldShouldReturn.
40: Close the BOOL.
As this is my first tutorial on this site, please provide feedback on it, as well as suggestions or comments. Also, if you notice something wrong, please post a comment or e-mail me about it. Thanks :).
Source code can be downloaded here:
ScrollView Source Code
Visit Steaps's Website
Visit Steaps on Twitter











Subscribe to Posts

One Response
Great tutorial. Would be nice to change to determine keyboard size without hard coding it (http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TextandWeb/TextandWeb.html#//apple_ref/doc/uid/TP40007072-CH20-SW7)
Thanks!