1290 1290 - 4 months ago 10
iOS Question

Moving textfield up with keyboard

I have this layout shown in the picture below:

enter image description here

I have this code to move the

Email Address
textfield up when the keyboard pops up:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.5];
[UIView setAnimationBeginsFromCurrentState:YES];
textfield1.frame = CGRectMake(textfield1.frame.origin.x, (textfield1.frame.origin.y - 100.0), textfield1.frame.size.width, textfield1.frame.size.height);
[UIView commitAnimations];
}


The only problem is when I move the textfield up like this it covers the
Email Login / Sign Up
Label right above it. Is there any way I can move the
Email Address
textfield up without covering that label? Or do I have to move every single view (such as the label) that is above that
Email Address
textfield up as well?

Answer

Consider observing the system notifications:

  • UIKeyboardWillShowNotification
  • UIKeyboardDidShowNotification
  • UIKeyboardWillHideNotification
  • UIKeyboardDidHideNotification

And check the documentation

You should move the view that wraps all the UITextFields (if no such view, just create a "form view" containing those fields). The userInfo property of the notification object contains the size of the keyboard. You can use that information and position the form view with a distance from the keyboard frame.