How to make a UITextField move up when keyboard is present

Posted by philfreo on Stack Overflow See other posts from Stack Overflow or by philfreo
Published on 2009-07-14T17:06:07Z Indexed on 2010/03/16 19:21 UTC
Read the original article Hit count: 615

Filed under:
|
|

EDIT: Over 1k views with 3 upvotes? If this question is helpful, vote it up :)

With the iPhone SDK:

I have a UIView with UITextFields that brings up a keyboard. I need it to be able to:

  1. Allow scrolling of the contents of the UIScrollView to see the other text fields once the keyboard is brought up
  2. Automatically "jump" (by scrolling up) or shortening

I know that I need a UIScrollView. I've tried changing the class of my UIView to a UIScrollView but I'm still unable to scroll the textboxes up or down.

Do I need both a UIView and a UIScrollView? Does one go inside the other? [EDIT: I now know that you want a UIView inside of a UIScrollView, and the trick is to programatically set the content size of the UIScrollView to the frame size of the UIView.]

Then what needs to be implemented in order to automatically scroll to the active text field?

Ideally as much of the setup of the components as possible will be done in Interface Builder. I'd like to only write code for what needs it.

Note: the UIView (or UIScrollView) that I'm working with is brought up by a tabbar (UITabBar), which needs to function as normal.


Edit: I am adding the scroll bar just for when the keyboard comes up. Even though it's not needed, I feel like it provides a better interface because then the user can scroll and change textboxes, for example.

I've got it working where I change the frame size of the UIScrollView when the keyboard goes up and down. I'm simply using:

-(void)textFieldDidBeginEditing:(UITextField *)textField { //Keyboard becomes visible
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, 
    							  scrollView.frame.size.width, scrollView.frame.size.height - 215 + 50); //resize
}

-(void)textFieldDidEndEditing:(UITextField *)textField { //keyboard will hide scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height + 215 - 50); //resize }

However this doesn't automatically "move up" or center the lower text fields in the visible area, which is what I would really like.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about iphone-sdk