Skip to content

Instantly share code, notes, and snippets.

@keicoder
Last active August 29, 2015 13:57
Show Gist options
  • Save keicoder/9359997 to your computer and use it in GitHub Desktop.
Save keicoder/9359997 to your computer and use it in GitHub Desktop.
objective-c : modify textView size when keyboard appears
//modify textView size when keyboard appears and disappears
//Add the following instance variable to NoteEditorViewController.m
@implementation NoteEditorViewController
{
//...
CGRect _textViewFrame; //used to store the current text view frame
}
//update viewDidLayoutSubviews to make use of this newly added instance variable
- (void)viewDidLayoutSubviews
{
//called to notify the view controller that view has just laid out subviews
//...
_textView.frame = _textViewFrame;
}
//add the following to the bottom of viewDidLoad to set the initial frame value
- (void)viewDidLoad
{
//...
_textViewFrame = self.view.bounds;
//...
}
//update _textViewFrame variable when the keyboard is shown
#pragma mark - UITextView 델리게이트 메소드
- (void)textViewDidBeginEditing:(UITextView *)textView
{
//텍스트 뷰 사이즈 조정
[UIView animateWithDuration:0.3 animations:^{
_textViewFrame = self.view.bounds;
_textViewFrame.size.height -= 216.0f; //reduces the height of the on-screen keyboard
_textView.frame = _textViewFrame;
}];
}
//return the text view frame to its original size when editing finishes
- (void)textViewDidEndEditing:(UITextView *)textView
{
//copy the updated note text to the underlying model
self.note.contents = textView.text;
_textViewFrame = self.view.bounds;
_textView.frame = _textViewFrame;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment