본문 바로가기

프로그래밍/아이폰 프로그래밍

18. 키보드가 뷰를 가릴때 스크롤 뷰 사용하기

이번시간에는 키보드가 올라와 뷰를 가릴때 가려져 있는 화면을 보기 위해 스크롤 뷰를 사용해 보기로 하겠습니다.


키보드가 올라와서 화면을 가리고 있습니다. 뷰에 스크롤 뷰를 넣어서 화면을 드래그 해서 볼수 있도록 해야겠습니다.


모든 컨트롤을 선택합니다. 그리고, Layout -> Embed Objects In -> Scroll View 옵션을 선택합니다.


ScrollView 필드를 선언하고 프로퍼티를 선언합니다.

DrinkDetailViewController.h

#import <UIKit/UIKit.h>



@interface DrinkDetailViewController : UIViewController {


NSDictionary *drink;

IBOutlet UITextField *nameTextField;

IBOutlet UITextView *ingredientsTextView;

IBOutlet UITextView *directionsTextView;

IBOutlet UIScrollView *scrollView;

}


@property (nonatomic,retain) NSDictionary *drink;

@property (nonatomic,retain) UITextField *nameTextField;

@property (nonatomic,retain) UITextView *ingredientsTextView;

@property (nonatomic,retain) UITextView *directionsTextView;

@property (nonatomic,retain) UIScrollView *scrollView;


@end





DrinkDetailViewController.m


@synthesize scrollView;



- (void)viewDidLoad {

    [super viewDidLoad];

// 스크롤 뷰의 사이즈를 뷰의 크기에 맞게 초기화 합니다.

scrollView.contentSize = self.view.frame.size;

}


- (void)dealloc {

[scrollView release];

    [super dealloc];

}





인터페이스 빌더에서 아웃렛에 연결 합니다.




키보드가 나올때, 들어갈때마다 아이폰은 이벤트를 알려줍니다. 그 이벤트를 받기 위해서 우리는 노티피케이션 센터에 등록해야합니다.
- 시스템 이벤트를 받기 위해서는 기본 노티피케이션 센터를 사용합니다.
- 자신이 통지 받을 객체로 등록할 것이므로 self를 지정합니다.
- 메소드 이름을 이용하여 셀렉터를 만들기 위해서는 @selector 키워드를 사용합니다. (콜론(:)을 반드시 사용)
- 어떤 객체에 의해서 키보드가 나타났는지는 신경쓰지 않으므로 object:nil

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



그리고 노티피케이션이 필요없어지면 등록 해지합니다. (뷰가 화면에서 사라지는 시점)

[[NSNotificationCenter defaultCenter] removeObserver:self];




AddDrinkViewController.m

- (void) viewWillAppear:(BOOL)animated {

[super viewWillAppear:(BOOL)animated];

NSLog(@"Registering for keyboard events");

[[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) viewWillDisappear:(BOOL)animated {

NSLog(@"Unregistering for keyboard events");

[[NSNotificationCenter defaultCenter] removeObserver:self];

}


- (void) keyboardDidShow: (NSNotification *)notif {

NSLog(@"Received UIKeyboardDidShowNotification");

// 이전에 키보드가 안보이는 상태였는지 확인합니다.

if (keyboardVisible) {

NSLog(@"Keyboard is already visible.");

return;

}

// 키보드의 크기를 읽어옵니다.

// NSNotification 객체는 userInfo 필드에 자세한 이벤트 정보를 담고 있습니다.

NSDictionary* info = [notif userInfo];

// 딕셔너리에서 키보드 크기를 얻어옵니다.

//NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey]; // deprecated

NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];

CGSize keyboardSize = [aValue CGRectValue].size;

// 키보드의 크기만큼 스크롤 뷰의 크기를 줄입니다.

CGRect viewFrame = self.view.frame;

viewFrame.size.height -= keyboardSize.height;

scrollView.frame = viewFrame;

keyboardVisible = YES;

}


- (void) keyboardDidHide: (NSNotification *)notif {

NSLog(@"Received UIKeyboardDidHideNotification");

// 이전에 키보드가 보이는 상태였는지 확인합니다.

if (!keyboardVisible) {

NSLog(@"Keyboard already hidden.");

return;

}

// 키보드의 크기를 읽어옵니다.

NSDictionary* info = [notif userInfo];

//NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];

NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

CGSize keyboardSize = [aValue CGRectValue].size;

// 키보드의 크기만큼 스크롤 뷰의 높이를 늘여서 원래의 크기로 만듭니다.

CGRect viewFrame = self.view.frame;

viewFrame.size.height += keyboardSize.height;

scrollView.frame = viewFrame;

keyboardVisible = NO;

}