LET me lOg

  • HOME
  • #iOS
  • Medium

키보드가 나타나거나 사라질 때 View 크기 조절 방법

키보드가 나타나거나 사라질 때 View 크기 조절 방법

회원가입 등 UITableViewCell에 subView로 UITextField, UITextView 등이 존재하고,
이를 터치했을 경우 키보드가 나타날 때 입력해야 할 공간이 키보드에 덮혀버리는 문제가 생기는데요.

이 때 위 문제를 해결하기 위해 여러 방법을 생각할 수 있을 것입니다.
UITableView를 안쓰면 되잖어! 라고 생각하시는 분도 계실테고 다른 꼼수를 이용하여 구현하시는 분도 계실 것입니다. 제가 구현한 방법은 아래와 같습니다.

1. ViewController의 기본 메소드 중 viewWill(Did)Appear, viewWill(Did)Disappear에서 Keyboard Notification의 Observer를 생성/삭제 합니다. name은 UIKeyboardWill(Did)ShowNotification, UIKeyboardWill(Did)HideNotification 으로 정의된 키보드 Notification을 View가 그려지기 전에 Observer에 등록하고, View가 사라지기 전에 삭제하였습니다.

- (void)viewWillAppear:(BOOL)animated   {
   [super viewWillAppear:animated];

   [[NSNotificationCenter defaultCenter] addObserver:self
                            selector:@selector(appearKeyboard:)
                            name:UIKeyboardDidShowNotification object:nil];

   [[NSNotificationCenter defaultCenter] addObserver:self
                            selector:@selector(disappearKeyboard:)
                            name:UIKeyboardDidHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated    {
   [super viewWillDisappear:animated];

   [[NSNotificationCenter defaultCenter] removeObserver:self
                            name:UIKeyboardDidShowNotification object:nil];

   [[NSNotificationCenter defaultCenter] removeObserver:self
                            name:UIKeyboardDidHideNotification object:nil];
}

2. 키보드 이벤트의 selector를 아래와 같이 정의합니다. 여기서 중요한 부분은 NSNotification에 정의된 userInfo 입니다. 키보드가 나타나는 이벤트의 duration, curve를 비롯하여 frame 정보가 이 userInfo에 정의되어 있습니다. 따라서 이 userInfo 값을 참조하여 키보드가 나타나거나 사라질 때 UITableView의 frame을 변경하고 선택한 row의 index 값을 참고하여 해당 indexPath로 scroll 해주는 것입니다.

- (void)appearKeyboard:(NSNotification *)noti   {

   CGRect frame = {0.0f, 0.0f, 0.0f, 0.0f};
   CGFloat animationDuration = 0.0f;

   [[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&frame];
   [[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]
                                               getValue:&animationDuration];

   _tableView.frame = CGRectMake(0.0f,0.0f,320.0f,416.0f-frame.size.height);

   [UIView animateWithDuration:animationDuration animations:^(void) {

   } completion:^(BOOL finished) {

      if (finished) {

         [_tableView scrollToRowAtIndexPath:
                  [NSIndexPath indexPathForRow:_selectedRow inSection:0]
                  atScrollPosition:UITableViewScrollPositionMiddle
                  animated:YES];
      }
   }];
}
- (void)disappearKeyboard:(NSNotification *)noti    {
   _tableView.frame = CGRectMake(0.0f,0.0f,320.0f,416.0f);
}

3. UITextField의 delegate 메소드는 아래와 같이 구현하였는데 입력을 위해 UITextField를 터치했을 때 터치된 UITextField의 부모를 참조하여 해당 cell의 index 값을 임시로 저장하였고, “완료”를 눌렀을 때는 키보드를 숨기기 위해 resignTextField 메소드를 호출하였습니다.

- (void)textFieldDidBeginEditing:(UITextField *)textField   {
   UITableViewCell *cell = (UITableViewCell *)[[textField superview] superview];
   NSIndexPath *indexPath = [_tableView indexPathForCell:cell];

   _selectedRow = indexPath.row;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField  {
   if ([textField isFirstResponder]) {
      [textField resignFirstResponder];
   }

   return NO;
}

생각보다 간단하죠? 테스트한 프로젝트도 함께 첨부합니다. 도움이 되셨으면 합니다.


공유:

  • 친구에게 전자우편으로 보내기 (새 창에서 열림)
  • 페이스북에 공유하려면 클릭하세요. (새 창에서 열림)
  • 구글 +1에서 공유하려면 클릭하세요 (새 창에서 열림)
  • 트위터로 공유하기 (새 창에서 열림)
  • LinkedIn으로 공유하기 (새 창에서 열림)
  • Tumblr로 공유하기 (새 창에서 열림)
  • Pinterest에서 공유하려면 클릭하세요 (새 창에서 열림)

관련

공유:

  • 친구에게 전자우편으로 보내기 (새 창에서 열림)
  • 페이스북에 공유하려면 클릭하세요. (새 창에서 열림)
  • 구글 +1에서 공유하려면 클릭하세요 (새 창에서 열림)
  • 트위터로 공유하기 (새 창에서 열림)
  • LinkedIn으로 공유하기 (새 창에서 열림)
  • Tumblr로 공유하기 (새 창에서 열림)
  • Pinterest에서 공유하려면 클릭하세요 (새 창에서 열림)

관련

  • Permalink
  • 2011-12-16
  • iOS Common, iOS Simple Tips

Comments (2)

  1. My Homepage - 응답

    2011-12-24 at 7:34 오후

    Hi there, just became aware of your weblog by way of Google, and identified that it is genuinely informative. I

    • yoonbong.kim - 응답

      2011-12-31 at 2:53 오후

      Thanks.

Leave a Comment - Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Twitter Feed
  • Oops, our Twitter feed is unavailable at the moment. - Follow us on Twitter!
태그
3D touch 2011 2012 ACAccount Apple Artwork backgroundImage beta CoreAnimation Customizing Facebook FTA H3 hack Hidden image Indicator iOS iOS4 iOS5 iOS5.1 iOS6 iOS7 iOS9 iPad iPhone kth naver Path2 QuadMenu Swift TechTalk Tips Twitter UI UIButton UINavigationBar UINavigationController UITabBarController UITextField y8k 국회의원 매국노 아임IN핫스팟 한미FTA
그 밖의 기능
  • 로그인
  • 글 RSS
  • 댓글 RSS
  • WordPress.org
  • yoonbong.kim@me.com
  • Github
  • Linkedin
  • Twitter
  • YoonBong Kim © 2015
loading 취소
글이 전송되지 않았습니다. 이메일 주소를 확인하세요!
이메일 확인에 실패했습니다. 다시 시도하세요
죄송합니다. 귀하의 블로그에서 이메일로 글을 공유할 수 없습니다.