Created
March 15, 2014 06:43
-
-
Save keicoder/9562687 to your computer and use it in GitHub Desktop.
objective-c : resize UITextView when showing keyboard
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
//resize UITextView when showing keyboard | |
- (void)keyboardWillShow:(NSNotification *)aNotification { | |
[self moveTextViewForKeyboard:aNotification up:YES]; | |
} | |
- (void)keyboardWillHide:(NSNotification *)aNotification { | |
[self moveTextViewForKeyboard:aNotification up:NO]; | |
} | |
- (void) moveTextViewForKeyboard:(NSNotification*)aNotification up: (BOOL) up | |
{ | |
NSDictionary* userInfo = [aNotification userInfo]; | |
// Get animation info from userInfo | |
NSTimeInterval animationDuration; | |
UIViewAnimationCurve animationCurve; | |
CGRect keyboardEndFrame; | |
[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve]; | |
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration]; | |
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame]; | |
// Animate up or down | |
[UIView beginAnimations:nil context:nil]; | |
[UIView setAnimationDuration:animationDuration]; | |
[UIView setAnimationCurve:animationCurve]; | |
CGRect newFrame = textView.frame; | |
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil]; | |
newFrame.size.height -= keyboardFrame.size.height * (up? 1 : -1); | |
textView.frame = newFrame; | |
[UIView commitAnimations]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment