Skip to content

Instantly share code, notes, and snippets.

@mjjimenez
Created December 14, 2013 09:04
Show Gist options
  • Save mjjimenez/7957125 to your computer and use it in GitHub Desktop.
Save mjjimenez/7957125 to your computer and use it in GitHub Desktop.
Autoscroll of TableView when not using TableViewController http://kingscocoa.com/tutorials/keyboard-content-offset/
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets;
if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
contentInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, 0.0, (keyboardSize.height), 0.0);
} else {
contentInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, 0.0, (keyboardSize.width), 0.0);
}
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
}
- (void)keyboardWillHide:(NSNotification *)notification
{
UIEdgeInsets edge = UIEdgeInsetsMake(0, 0, 0, 0);
self.tableView.contentInset = edge;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.inputView.stringTag = textField.stringTag;
UITableViewCell *pickerCell = (UITableViewCell*)textField.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:pickerCell];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment