티스토리 뷰
로그인 창을 구성할 경우에, 키보드가 나오면서 로그인 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' 카테고리의 다른 글
NSDate to NSString 형변환 (0) | 2015.09.23 |
---|---|
UITebleView row 삭제할 때 (0) | 2015.09.22 |
[ios] Find the current top view controller for your iOS application (0) | 2015.06.05 |
[ios] milliseconds to NSDate (0) | 2015.04.28 |
xcode에서 앱 아이콘 사이즈, 파일명 정리 (0) | 2015.04.06 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 킹스맨
- serialize
- ListView
- cocoapods
- 안드로이드 라이브러리
- gitgub
- 막국수
- 리엔케이
- 아이콘 사이즈
- 코드 하이라이트
- keyboard
- custom font
- 봄봄봄
- 휴게소
- Xcode
- xcode8
- Android
- 남자화장품
- ShardPreferenes
- 참나무숯불닭갈비
- crop
- android jar
- resize
- git flow
- 가평휴게소
- 안드로이드
- xcode7
- 엘지
- ios
- Re:NK
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함