Posted By

yasirmturk on 08/20/11


Tagged

iphone UITextField


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

yasirmturk
cnathan


Move up the UIView with UITextField s under the keyboard


 / Published in: Objective C
 

URL: http://yasirmturk.com/blog

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

  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. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: yasirmturk on August 20, 2011

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

You need to login to post a comment.