How can I make a UITextField move up when the keyboard appears?

Introduction On Keyboard appearing in Mobile App

In iPhone or iPad, screen size limited but sometime our design of app becomes too large in comparison to their screen size, in those cases we use UIScrollView or UITableView or any other element as per our design requirements and make content scrollable or draggable thats how we consume small screen size to fit our design and yes thats also a very challenging part as giving scrollable content means you have a lot of things to manage their inner elements size and their respective functionality and their actions as well. And here now we will get to learn about solving one of its challenge that is to manage UITextField in UITableView while keyboard is appears and disappears.

UITextField Change Position Keyboard Appear And Disappear

Overview for UITextField / TextInput Move When Keyboard Appears

For implementing this solution you must know NSNotificationCenter and constraints- app design and just nothing but basic calculations. A notification dispatch mechanism that enables the broadcast of information to registered observers. For More details about NSNotificationCenter (

Solution for UITextField / TextInput Move When Keyboard Appears

First initialisation of variables :-
CGFloat tblViewHeight;

And then create an outlet for UITableView bottom margin constraint. In storyboard, where our view controller is there and UITableView is placed in uiviewcontroller with constraints and the constraint that connects bottom of UITableView to bottom of uiviewcontroller we will attach that constaint to this outlet ‘tableBottomMargin’ that we are going to create.
IBOutlet NSLayoutConstraint *tableBottomMargin;

We will start with making two functions as below

// keyboard show
- (void)keyboardWillShow:(NSNotification *)sender {
CGSize kbSize = [[[sender userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
NSTimeInterval duration = [[[sender userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// CGRect tblFrame = [self.tblRecords frame];
// float tableBottomMarging = kbSize.height - (( DEVICE_HEIGHT - tblFrame.origin.y ) - tblFrame.size.height);
float tableBottomMarging = kbSize.height + 5.0;
NSLog(@"tablebottomMarging = %f",tableBottomMarging);
if(tableBottomMarging > tblViewHeight) {
[UIView animateWithDuration:duration animations:^{
tableBottomMargin.constant = tableBottomMarging;

// keyboard hide
- (void)keyboardWillHide:(NSNotification *)sender {
NSTimeInterval duration = [[[sender userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
NSLog(@"tblViewHeight = %f",tblViewHeight);
[UIView animateWithDuration:duration animations:^{
tableBottomMargin.constant = tblViewHeight;

And after this we will add observer for keyboard appear and keyboard disappears in viewWillAppear methods

- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

And after add observer we will make sure that these observer will removes when screen exits.
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:YES];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

Leave a Comment