티스토리 뷰

#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

최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함