Monday 8 April 2013

How to move the view when keyboard pops up


When the user clicks on the textfield present at the bottom of the view, suddenly keyboard pops up and hides the field, so that the user can't able to see what he/she is typing.

 So, to avoid such situation we can able to move the view when keyboard pops up.

Here is the coding that will be useful for,

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += -215;  /*specify the points to move the view up*/
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:-10];
    
    [self.view setFrame:viewFrame];
    
    [UIView commitAnimations];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += 215;   /*specify the points to move the view down*/
    
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:-10];
    
    [self.view setFrame:viewFrame];
    
    [UIView commitAnimations];
}

No comments:

Post a Comment