how to make a UITextField move up when the keyboard is presented

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.

http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present


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.
flag

   

12 Answers

9

1) You will need a scroll view if the contents you have now does not fit in iphone screen. [If you are adding the scroll view just to make the textfield scroll up when keyboard comes up, then its not needed]

2) For showing the textfields without being hidden by the keyboard, the standard way is to move up/down the view having textfields whenever the keyboard is shown

here is a sample code

-(void)textFieldDidBeginEditing:(UITextField*)sender
{
   
if([sender isEqual:_textField])
   
{
       
//move the main view, so that the keyboard does not hide it.
       
if  (self.view.frame.origin.y >=0)
       
{
           
[self setViewMovedUp:YES];
       
}
   
}
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
   
[UIView beginAnimations:nil context:NULL];
   
[UIView setAnimationDuration:0.5];// if you want to slide up the view

   
CGRect rect =self.view.frame;
   
if(movedUp)
   
{
       
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
       
// 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect
.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect
.size.height += kOFFSET_FOR_KEYBOARD;
   
}
   
else
   
{
       
// revert back to the normal state.
        rect
.origin.y += kOFFSET_FOR_KEYBOARD;
        rect
.size.height -= kOFFSET_FOR_KEYBOARD;
   
}
   
self.view.frame = rect;

   
[UIView commitAnimations];
}


-(void)keyboardWillShow:(NSNotification*)notif
{
   
//keyboard will be shown now. depending for which textfield is active, move up or move down the view appropriately

   
if([_textField isFirstResponder]&&self.view.frame.origin.y >=0)
   
{
       
[self setViewMovedUp:YES];
   
}
   
elseif(![_textField isFirstResponder]&&self.view.frame.origin.y <0)
   
{
       
[self setViewMovedUp:NO];
   
}
}


-(void)viewWillAppear:(BOOL)animated
{
   
// register for keyboard notifications
   
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
                                                                                         name
:UIKeyboardWillShowNotificationobject:self.view.window];
}

-(void)viewWillDisappear:(BOOL)animated
{
     
// unregister for keyboard notifications while not visible.
   
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotificationobject:nil];
}

define kOFFSET_FOR_KEYBOARD to a value as needed. like

#define kOFFSET_FOR_KEYBOARD 60.0

Hope this helps



출처 : http://blog.naver.com/PostList.nhn?blogId=philipousys¤tPage=41
Posted by 오늘마감

티스토리 툴바