Skip to content

Instantly share code, notes, and snippets.

@wagyu298
Created June 18, 2013 01:58
Show Gist options
  • Save wagyu298/5802089 to your computer and use it in GitHub Desktop.
Save wagyu298/5802089 to your computer and use it in GitHub Desktop.
This is a code snippet to control UITextView's contentInset.bottom and scrollIndicatorInset.bottom when iPhone's keyboard status is changed to visible or hide.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[notificationCenter addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[notificationCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary *info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
double duration = [(NSNumber *)[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
unsigned int curve = [(NSNumber *)[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] unsignedIntValue];
UIEdgeInsets contentInset = self.textView.contentInset;
UIEdgeInsets scrollInset = self.textView.scrollIndicatorInsets;
contentInset.bottom = kbSize.height;
scrollInset.bottom = kbSize.height;
[UIView animateWithDuration:duration
delay:0.0
options:curve
animations:^{
self.textView.contentInset = contentInset;
self.textView.scrollIndicatorInsets = scrollInset;
}
completion:nil];
}
- (void)keyboardWillHide:(NSNotification*)aNotification
{
NSDictionary *info = [aNotification userInfo];
double duration = [(NSNumber *)[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
unsigned int curve = [(NSNumber *)[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] unsignedIntValue];
UIEdgeInsets contentInset = self.textView.contentInset;
UIEdgeInsets scrollInset = self.textView.scrollIndicatorInsets;
contentInset.bottom = 0;
scrollInset.bottom = 0;
[UIView animateWithDuration:duration
delay:0.0
options:curve
animations:^{
self.textView.contentInset = contentInset;
self.textView.scrollIndicatorInsets = scrollInset;
}
completion:nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment