키보드가 나타나면 바로 밑(키보드)에 있는 화면들은 보이지 않게 된다. 이때 이용하는 것이 스크롤뷰이다.
Step 1) 우선 화면에 있는 컴포넌트들 (레이블, 텍스트 등등)을 포함하는 스크롤 뷰를 추가한 후
Step 2) NotificationCenter에 키보드가 나타나고 사라질 때의 콜백 함수를 등록한다.
Step 3) 키보드가 나타나는 콜백 함수가 불려지면 스크롤 뷰의 프레임의 높이를 키보드 높이만큼 줄여서 스크롤바가 만들어지도록 한다.
Step 4) 만약 키보드가 사라지는 콜백 함수가 불려지면 스크롤 뷰의 프레임 높이를 키보드 높이만큼 추가하여 원래 프레임 사이즈로 되돌릴 수 있게 한다.
Step 1). 해당 뷰의 xib 파일을 연다.
뷰 화면에서 애플키 + A를 눌러서 모든 GUI 컴포넌트를 선택 한 후
메뉴에서 [Layout] -> [Embedded objects in] -> [Scroll View]
를 눌러 스크롤 뷰를 만든다.
Step 2,3,4)
헤더 파일에 다음과 같이 추가한다.
...
// to decide whether keyboard is already shown
// if already shown, don' change the size of the frame of the scroll view
BOOL keyboardVisible;
IBOutlet UIScrollView *uiScrollView;
...
@property (nonatomic, retain) UIScrollView *uiScrollView;
...
// callbacks functions when keyboard is shown or hide
- (void) keyboardDidShow:(NSNotification *) notif;
- (void) keyboardDidHide:(NSNotification *) notif;
구현 파일에 다음과 같이 추가한다.
@synthesize uiScrollView;
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Step 2)
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
keyboardVisible = NO;
}
- (void) keyboardDidShow:(NSNotification *) notif {
NSLog(@"keyboardDidShow");
if(keyboardVisible)
return;
// Step 3)
// Get the size of the keyboard
NSDictionary *info = [notif userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = self.view.frame;
viewFrame.size.height -= keyboardSize.height;
uiScrollView.frame = viewFrame;
keyboardVisible = YES;
}
- (void) keyboardDidHide:(NSNotification *) notif {
if(!keyboardVisible)
return;
NSLog(@"keyboardDidHide");
// Step 4)
// Get the size of the keyboard
NSDictionary *info = [notif userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue].size;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = self.view.frame;
viewFrame.size.height += keyboardSize.height;
uiScrollView.frame = viewFrame;
keyboardVisible = NO;
}
- (void) viewWillDisappear:(BOOL)animated {
NSLog(@"viewWillDisappear");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)dealloc {
[uiScrollView release];
[super dealloc];
}
출처 : http://blog.daum.net/shakalis/35
'프로그램 > iPhone' 카테고리의 다른 글
UIDatepicker를 Popup 효과 내기 (0) | 2011.01.17 |
---|---|
Create custom activity indicator for your iPhone App (0) | 2011.01.13 |
iPhone - combobox (0) | 2011.01.12 |
tableView에서 페이지 (0) | 2011.01.12 |
iPhone UIButton 을 UISwitch(체크박스)처럼 이용하기 (0) | 2011.01.11 |