iphoneobjective-ccocoa-touchuikeyboard

Making the view slide up to make room for the keyboard?


I just started learning to program iPhone apps and I can't seem to figure out how to make the view slide out of the way when the keyboard appears (so you can still see the text field you're typing in). How is it done?


Solution

  • If it is OK visually, the easiest is to move the entire self.view.frame and then move it back down when finished.

    static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3f;
    
    - (void) animateForToNewYPosition:(int)newYPosition {
        // move for kdb
        if (self.view.frame.origin.y == newYPosition) {
            return;
        }
    
        // start animation
        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];  
    
        // move it
        self.view.frame.origin.y = newYPosition;
    
        [UIView commitAnimations];  
    }