1

How to move View up when Keyboard appeared in small screen like 4s, 5 and 5s but view should not move up when its enter inside iphone 6 and 6S.

I am sharing my code which is i have done in my app for all screen but in my condition view should not move up when its enter in 6 or 6s.

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField:textField up:YES];
}

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

-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
    const int movementDistance = -60; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed

    int movement = (up ? movementDistance : -movementDistance);

    [UIView beginAnimations: @"animateTextField" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    [UIView commitAnimations];
}
Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
Pranshu Singh
  • 73
  • 1
  • 10

2 Answers2

4

I suggest you use IQKeyboardManager It will manage everything for you with less efforts

Rahul Patel
  • 1,822
  • 12
  • 21
1

// Add a scrollview on main view and add UITextField on that scrollview

-(void) viewDidLoad
{
  UIScrollView  *myScrollView =  [ [UIScrollView  alloc]  initWithFrame:[UIScreen mainScreen ].bounds];
  [myScrollView .contentSize =CGSizeMake(320, 500);
  [myScrollView .contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
  [self.view addSubView:myScrollView ];

  UITextField *myTextField = [ [UITextField  alloc] initWithFrame:CGRectMake(20,30,100,33)];
  [myScrollView addSubView:myTextField ];
   myTextField.delegate = self;
}

// Set the scrollview content offset to make the myTextField move up

  - (void) textFieldDidBeginEditing:(UITextField *)textField
{
  [myScrollView setContentOffset:CGPointMake(0,textField.center.y-80) animated:YES]; 
  // here '80' can be any number which decide the height that textfiled should move
}

//To move the textfield to its original position

- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
  [[myScrollView  setContentOffset:CGPointMake(0,0) animated:YES];
  [textField resignFirstResponder];
  return YES;
}
Teja Kumar Bethina
  • 3,486
  • 26
  • 34