---
title: "How to make a UITextField move up when keyboard is present"  
description: "How to make a UITextField move up when keyboard is present"  
author: "Anonymous User"  
published: 2015-04-25  
updated: 2015-04-25  
canonical: https://www.mindstick.com/forum/23137/how-to-make-a-uitextfield-move-up-when-keyboard-is-present  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 5 minutes  

---

# How to make a UITextField move up when keyboard is present

I have a UIView with UITextFields that brings up a [keyboard](https://www.mindstick.com/forum/23117/android-soft-keyboard-close-hide). 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](https://www.mindstick.com/articles/13019/get-the-best-content-marketing-pricing-packages-for-your-brand) 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](https://answers.mindstick.com/qa/97113/why-does-an-active-ftp-not-work-with-network-firewalls) [text field](https://www.mindstick.com/forum/33753/how-to-get-value-from-text-field-on-button-click-in-ios)?\
Ideally as much of the setup of the [components](https://www.mindstick.com/blog/74096/understanding-the-role-of-some-integral-ac-components) as possible will be done in [Interface](https://www.mindstick.com/articles/12101/interfaces-in-java-extending-interfaces) 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](https://www.mindstick.com/forum/212/can-be-declare-a-static-function-as-virtual) normal.\
\
\
Edit: I am adding the scroll bar just for when the keyboard [comes up](https://answers.mindstick.com/qa/45369/if-you-search-worst-bollywood-actor-on-google-guess-whose-name-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](https://www.mindstick.com/forum/160255/what-is-the-significance-of-the-up-and-down-methods-in-a-migration-file). 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.\

## Replies

### Reply by Anonymous User

1.You will need a scroll view if the contents you have now does not fit in the iPhone screen. (If you are adding the scroll view just to make the textfield scroll up when keyboard comes up, then it's 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 some sample code:\

```
#define kOFFSET_FOR_KEYBOARD 80.0-(void)keyboardWillShow {    // Animate the current view out of the way    if (self.view.frame.origin.y >= 0)    {        [self setViewMovedUp:YES];    }    else if (self.view.frame.origin.y < 0)    {        [self setViewMovedUp:NO];    }}-(void)keyboardWillHide {    if (self.view.frame.origin.y >= 0)    {        [self setViewMovedUp:YES];    }    else if (self.view.frame.origin.y < 0)    {        [self setViewMovedUp:NO];    }}-(void)textFieldDidBeginEditing:(UITextField *)sender{    if ([sender isEqual:mailTf])    {        //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.3]; // 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)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];    // register for keyboard notifications    [[NSNotificationCenter defaultCenter] addObserver:self                                         selector:@selector(keyboardWillShow)                                             name:UIKeyboardWillShowNotification                                           object:nil];    [[NSNotificationCenter defaultCenter] addObserver:self                                         selector:@selector(keyboardWillHide)                                             name:UIKeyboardWillHideNotification                                           object:nil];}- (void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    // unregister for keyboard notifications while not visible.    [[NSNotificationCenter defaultCenter] removeObserver:self                                             name:UIKeyboardWillShowNotification                                           object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self                                             name:UIKeyboardWillHideNotification                                           object:nil];}
```


---

Original Source: https://www.mindstick.com/forum/23137/how-to-make-a-uitextfield-move-up-when-keyboard-is-present

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
