How To Slide Views With Animation When Keypad Is Present iOS

Ok, so we are here to know how to slide the whole screen (the views: like UITextField) when keypad is present. Have you experienced when your cursor is inside the UITextField and then the Keypad presents but the textfield is now being covered by the keypad? Haha! You are not alone. I've experienced that. So we need to slide up the whole screen to fix the problem.

Check out the codes below:

- (void)dismissKeyboard
    [self.view endEditing:YES];

UITextField Delegates (Oh, I forgot to mention that you need to set the delegate of your UITextFields. Set the delegates to self, also put the UITextField Class delegate to your .h file.
- (void)textFieldDidBeginEditing:(UITextField *)textField
    [self animateTextField: textField up: YES];

- (void)textFieldDidEndEditing:(UITextField *)textField 
    [self animateTextField: textField up: NO];

Now, for the ANIMATION part:
- (void)animateTextField: (UITextField*) textField up: (BOOL) up
    const int movementDistance = 80; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed
    int movement = (up ? -movementDistance : movementDistance);
    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];

If the tutorial is not clear enough for you, please feel free to shoot me a question or your questions by commenting below of the post or contacting me.

Post a Comment