Post Pic

How to Scroll an Object into View

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:

Textfield Begin Editing

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:

Textfield Begin Editing

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

Related Posts

  • Animating a View in iPhone SDK


  • One Response

    07.01.09

    Leave Your Response

    * Name, Email, Comment are Required
    Subscribe via RSS

    Receive the latest iPhone app tutorials and resources delivered to you. Free.

    Latest Tweet



    Follow us on Twitter

    Write for Us

    Get your name in front of the masses by writing for iPhone App Tuts. We will share a plug about you and give links to your site and Twitter page.

    Write for Us

    We need your feedback

    Would you pay a small monthly subscription for consistent iPhone tutorials?

    View Results

    Loading ... Loading ...