Move up the UIView with UITextField s under the keyboard


/ Published in: Objective C
Save to your folder(s)

Add this code to the controller
Set controller as the delegate of the all the UITextField s in the UIView


Copy this code and paste it in your HTML
  1. - (void) animateTextField: (UITextField*) textField up: (BOOL) up{
  2. int txtPosition = (textField.frame.origin.y - 140);
  3. const int movementDistance = (txtPosition < 0 ? 0 : txtPosition); // tweak as needed
  4. const float movementDuration = 0.3f; // tweak as needed
  5.  
  6. int movement = (up ? -movementDistance : movementDistance);
  7.  
  8. [UIView beginAnimations: @"anim" context: nil];
  9. [UIView setAnimationBeginsFromCurrentState: YES];
  10. [UIView setAnimationDuration: movementDuration];
  11. self.view.frame = CGRectOffset(self.view.frame, 0, movement);
  12. [UIView commitAnimations];
  13. }
  14.  
  15. - (void)textFieldDidBeginEditing:(UITextField *)textField{
  16. [self animateTextField: textField up: YES];
  17. }
  18.  
  19. - (void)textFieldDidEndEditing:(UITextField *)textField{
  20. [self animateTextField: textField up: NO];
  21. }
  22.  
  23. - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
  24. [theTextField resignFirstResponder];
  25. return YES;
  26. }

URL: http://yasirmturk.com/blog

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.