'UIKeyboard'에 해당되는 글 1건

  1. 2010.12.13 [IOS] KEYBOARD 화면 이동
로그인 창을 구성할 경우에, 키보드가 나오면서 로그인 UITextField영역을 덮어 버리는 경우가 있다. 이 경우 화면설계를 할 때 하단에 키보드가 나왔을 때의 영역을 고려해서 로그인 텍스트 필드를 배치하는 경우도 있다.하지만, 임시방편일 뿐이고, 보다 근본적인 방법으로 키보드가 나올 때 해당 UIView를 위로 올렸다가 키보드가 사라질 때 그걸 내리는 방법으로 구현해야 한다.

이 방법을 좀 더 세부적으로 보면
1. UIKeyboard가 나타나는 이벤트를 감지했을 때
2. 키보드 아래쪽의 UIView영역을 키보드 높이 만큼 위로 이동시킨다.
3. 키보드 입력이 모두 끝나, 키보드 닫는 이벤트가 나왔을 때
4. 키보드를 사라지게 만드면서, UIView를 원래의 위치로 내린다.

구현을 위해서,

먼저 UIKeyboardWillShowNotification 이벤트가 발생할 때, keyboardWillShow 메소드를 호출하도록 등록한다.
- (void)loadView
{    
    [super loadView];    
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification 
                                               object:self.view.window];
}
keyboardWillShow 메소드는 다음과 같은 형태이다.

- (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];
    }
}



해당 notification이 발생했을 때 위 메소드가 실행되고, 
키보드의 상태에 따라서 View를 올릴지 내릴지 애니메이션을 시켜주는 setViewMovedUp 메소드를 호출해준다.

- (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];
}



UIView의 애니메이션을 어떻게 시키는지 보여주는 메소드이다.


마지막으로 키보드 작업이 끝났을 때에 키보드를 내려주도록 textFieldDidEndEditing 메소드를 구현해주도록 하자.

- (void)textFieldDidEndEditing:(UITextField *)myTextField;
{
    if( self.view.frame.origin.y < 0 )
    {
        [self setViewMovedUp:NO];
    }
}


이제 키보드가 나타날 때 그 아래의 뷰도 함께 이동시킬 수 있게 되었다.

출처 :  http://alanduncan.net/old/index.php?q=node/13

'개발 > iOS' 카테고리의 다른 글

[iOS] XCode gloss effect 없애기  (0) 2012.03.24
[iOS] UITableView Cell 변경 animation  (0) 2011.04.29
[IOS] image size  (0) 2010.11.28
[XCODE] Breakpoint가 안먹을 때  (0) 2010.11.28
[XCODE] 메모리 관리 규칙  (0) 2010.11.16
Posted by 나랑살자
,