#define kOFFSET_FOR_KEYBOARD 150.0 // 키보드 높이


    [[NSNotificationCenter defaultCenter] addObserver:self 

                                             selector:@selector(keyboardWillShow:) 

                                                 name:UIKeyboardWillShowNotification 

                                               object:self.view.window];
 

    // 생성자 또는 초기화시 실행되는 위치치에 두어야 하는 코드

    [[NSNotificationCenter defaultCenter] addObserver:self 

                                             selector:@selector(keyboardWillShow:) 

                                                 name:UIKeyboardWillShowNotification 

                                               object:self.view.window];

 

#pragma mark - virual keyboard

- (void)keyboardWillShow:(NSNotification *)notif

{

    // The keyboard will be shown. If the user is editing the author, adjust the display so that the

    // author field will not be covered by the keyboard.

    if ( [[self userIDTextField] isFirstResponder] && self.view.frame.origin.y >= 0) {

        [self setViewMovedUp:YES];

    } else if ( [[self userIDTextField] isFirstResponder] && self.view.frame.origin.y < 0) {

        [self setViewMovedUp:NO];

    }

    [self setViewMovedUp: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;

        

    } else {

        // If moving down, not only increase the origin but decrease the height.

        rect.origin.y += kOFFSET_FOR_KEYBOARD;

        rect.size.height -= kOFFSET_FOR_KEYBOARD;

    }

    self.view.frame = rect;

    [UIView commitAnimations];

}


- (void)textFieldDidEndEditing:(UITextField *)myTextField;

{

    if( self.view.frame.origin.y < 0 ) {

        [self setViewMovedUp:NO];

    }

}

출처: http://blog.mongee.net/22

posted by ned3y2k