Created
March 3, 2014 12:44
-
-
Save keicoder/9324187 to your computer and use it in GitHub Desktop.
objective-c : adjust UITextView size and scrolling position
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//adjust UITextView size and scrolling position | |
- (void)viewDidLoad | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
[super viewDidLoad]; | |
[self registerForKeyboardNotifications]; | |
} | |
- (void)viewWillDisappear:(BOOL)animated | |
{ | |
if (debug==1) {NSLog(@"Running %@ '%@'", self.class, NSStringFromSelector(_cmd));} | |
[super viewWillDisappear:animated]; | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; //키보드 옵저버 해제 | |
} | |
#pragma mark - Keyboard Notifications | |
// Call this method somewhere in your view controller setup code. | |
- (void)registerForKeyboardNotifications | |
{ | |
//키보드 팝업 옵저버 (키보드 팝업 시 텍스트 뷰 사이즈 조절) | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) | |
name:UIKeyboardWillShowNotification object:self.view.window]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) | |
name:UIKeyboardWillHideNotification object:self.view.window]; | |
} | |
- (void)updateNoteTextViewSize { | |
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; | |
CGFloat keyboardHeight = UIInterfaceOrientationIsLandscape(orientation) ? _keyboardSize.width : _keyboardSize.height; | |
self.noteTextView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - keyboardHeight); | |
} | |
// Called when the UIKeyboardDidShowNotification is sent. | |
- (void)keyboardWillShow:(NSNotification *)notification | |
{ | |
NSDictionary *userInfo = [notification userInfo]; | |
_keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; | |
//노트필드 사이즈 조정 | |
double uDelayInSeconds = 0.4; | |
dispatch_time_t uPopTime = dispatch_time(DISPATCH_TIME_NOW, uDelayInSeconds * NSEC_PER_SEC); | |
dispatch_after(uPopTime, dispatch_get_main_queue(), ^(void){ | |
[UIView animateWithDuration:0.3 animations:^{ | |
[self updateNoteTextViewSize]; | |
}]; | |
}); | |
} | |
// Called when the UIKeyboardWillHideNotification is sent | |
- (void)keyboardWillHide:(NSNotification*)aNotification | |
{ | |
_keyboardSize = CGSizeMake(0.0, 0.0); | |
self.noteTextView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); //노트필드 컨텐츠 인 셋 (top, left, bottom, right) | |
self.noteTextView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, 0); //노트필드 스크롤 인디케이터 인 셋 | |
[UIView animateWithDuration:0.3 animations:^{ | |
[self updateNoteTextViewSize]; | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment