4

In my app, when I click on a text field, the keyboard hides it. Please help me -- how can I move my view up when I click on the text field. I'm using this code in textFieldDidBeginEditing:

self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 216, 0);
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);

but it doesn't work.

jscs
  • 62,161
  • 12
  • 145
  • 186
sandy
  • 1,798
  • 3
  • 17
  • 23
  • possible duplicate of [How to make a UITextField move up when keyboard is present](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – jscs May 13 '11 at 05:54

2 Answers2

13

You should not trust textFieldDidBeginEditing: to adjust for the keyboard, since this method will be called even if the user is typing using a physical keyboard where an onscreen keyboard will not be displayed.

Instead listen to the UIKeyboardWillShowNotification, that is only triggered when the keyboard will actually be displayed. You need to do a three step process:

  1. Determine actual size of keyboard from the notifications userInfo dictionary. The size will differ from landscape/portrait, and different devices.
  2. Update the contentInset using the determined size. You can do it animated, the notification will even tell you the duration for the keyboard animation.
  3. Scroll the textfield into view, very easy to forget this!

You find more information and sample code from here

swiftBoy
  • 33,793
  • 26
  • 129
  • 124
PeyloW
  • 36,308
  • 12
  • 75
  • 98
  • kindly show me code to know the size of keyboard in landscape and potraite – sandy May 11 '11 at 14:41
  • @Sandy: The complete code, with working example is available at the link on developer.apple.com that I linked to in the answer. – PeyloW May 11 '11 at 14:46
  • Keep in mind that the Apple example forgets to work with the height of the textfield. Resulting in fields bordering on the keyboard not being shown or not shown completely. – ophychius Jul 16 '12 at 11:25
1

You can do the following, but first make sure you've set the UITextField delegate to your self and

#define kOFFSET_FOR_KEYBOARD 350;

at the top. This is how far you want the view to be shifted

//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
    [UIView setAnimationBeginsFromCurrentState:YES];

    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.

        if (rect.origin.y == 0 ) {
            rect.origin.y -= kOFFSET_FOR_KEYBOARD;
            //rect.size.height += kOFFSET_FOR_KEYBOARD;
        }

    }
    else
    {
        if (stayup == NO) {
            rect.origin.y += kOFFSET_FOR_KEYBOARD;
            //rect.size.height -= kOFFSET_FOR_KEYBOARD;
        }
    }
    self.view.frame = rect; 
    [UIView commitAnimations];
}


- (void)keyboardWillHide:(NSNotification *)notif {
    [self setViewMovedUp:NO];
}


- (void)keyboardWillShow:(NSNotification *)notif{
    [self setViewMovedUp:YES];
}


- (void)textFieldDidBeginEditing:(UITextField *)textField {
    stayup = YES;
    [self setViewMovedUp:YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    stayup = NO;
    [self setViewMovedUp:NO];
}

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

- (void)viewWillDisappear:(BOOL)animated
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
aherlambang
  • 14,092
  • 49
  • 146
  • 246
  • 4
    This example hare several drawbacks, based on a few faulty assumptions; never physical keyboard, only portrait mode, Apple will never change size of keyboard, or length of animation. It can also be impossible to scroll to content that is offsetted, if the user would like to for example change between textfields without first dismissing the keyboard. – PeyloW May 11 '11 at 14:24
  • thanx but i don't want to move my view for all textfield, so kindly tell me how do i select particular textfield – sandy May 11 '11 at 14:30
  • hello,I didn't get how to prevent my view to move up where keyboard does not hide my textfield. pls help me – sandy May 12 '11 at 11:10
  • 1
    A #define should not have a semi-colon behind it – Toad Dec 14 '12 at 14:03