I have a tab controller based app, where I have a uitextview on selecting which I m moving view up as keyboard appears , the entire thing works well until I switch tab , when I come back to this tab and try editing in uitextview view moves down and then back to normal position on keyboard appearing instead of moving up . here is the entire code

- (void)textViewDidBeginEditing:(UITextView *)textField
{
[self keyboardWillShow];

}
-(void)textViewDidEndEditing:(UITextView *)textField
{
[self keyboardWillHide];
}
 -(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
    [self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
    [self setViewMovedUp:NO];
}
}

-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
    [self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
    [self setViewMovedUp:NO];
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; // if you want to slide up the view

CGRect rect = self.view.frame;
if (movedUp)
{
    // 1. move the view's origin up so that the text field that will be hidden come above the    keyboard
    // 2. increase the size of the view so that the area behind the keyboard is covered up.
    rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
    // revert back to the normal state.
    rect.origin.y += kOFFSET_FOR_KEYBOARD;
    rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;

[UIView commitAnimations];
}

Related posts

Recent Viewed