If you have ever used a UITextField on the iPhone and seen the screen ’slide’ up to keep the UITextField in view, well here is how I’ve done it.
(This is for Portrait orientation only)
In your “ViewController.h” place 2 constants:
CODE
static const CGFloat FKEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat FPORTRAIT_KEYBOARD_HEIGHT = 216;
Then in your “ViewController.m” place 2 voids:
CODE
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
animatedDistance = floor(textFieldRect.origin.y - FPORTRAIT_KEYBOARD_HEIGHT);
if( animatedDistance > 0){
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:FKEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
}else{
animatedDistance = 0;
}
[UIView commitAnimations];
}
and
CODE
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Then in interface builder set each UITextField’s delegate to “File’s Owner”.
The textFieldShouldReturn make the keyboard close, the other handles shifting 'up' to display to keep the textfield in view while your typing