Thursday, July 1, 2010

Keyboard adjustment while typing in a textfield

- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self setViewMovedUp:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[self setViewMovedUp:NO];
return YES;
}

- (void)setViewMovedUp:(BOOL)movedUp{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
// Make changes to the view's frame inside the animation block. They will be animated instead
// of taking place immediately.
CGRect rect = self.view.frame;
if (movedUp){
// If moving up, not only decrease the origin but increase the height so the view
// covers the entire screen behind the keyboard.
//rect.origin.y -= kOFFSET_FOR_KEYBOARD;
//rect.size.height += kOFFSET_FOR_KEYBOARD;
if(rect.origin.y ==0)
rect.origin.y = self.view.frame.origin.y - 165;
}
else{
// If moving down, not only increase the origin decrease the height.
//rect.origin.y += kOFFSET_FOR_KEYBOARD;
//rect.size.height -= kOFFSET_FOR_KEYBOARD;
if(rect.origin.y <0)
rect.origin.y = self.view.frame.origin.y + 165;
}

self.view.frame = rect;
[UIView commitAnimations];
}

No comments:

Post a Comment