#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
@synthesize scrollView;
- (void)viewDidLoad {
[super viewDidLoad];
// 스크롤 뷰의 사이즈를 뷰의 크기에 맞게 초기화 합니다.
scrollView.contentSize = self.view.frame.size;
}
- (void)dealloc {
[scrollView release];
[super dealloc];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self];
- (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;
}