Skip to content

Instantly share code, notes, and snippets.

@link82
Forked from tudormunteanu/gist:6365593
Created August 28, 2013 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save link82/6365611 to your computer and use it in GitHub Desktop.
Save link82/6365611 to your computer and use it in GitHub Desktop.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
return self;
}
- (void) keyboardDidShow:(NSNotification *)notif {
float newHeight = 240;
if (self.view.frame.size.height > 480) {
newHeight = 300;
}
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, newHeight);
self.tableView.frame = self.view.frame;
NSArray *fields = [self.formModel findTextFields];
UITextField *focusField = nil;
for (UITextField *f in fields) {
if ([f isEditing]) {
focusField = f;
break;
}
}
UITableViewCell *focusCell = (UITableViewCell *)[[focusField superview] superview];
NSIndexPath *focusIndexPath = [self.tableView indexPathForCell:focusCell];
[self.tableView scrollToRowAtIndexPath:focusIndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
- (void) keyboardWillHide:(NSNotification *)notif {
self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.initialViewHeight);
self.tableView.frame = self.view.frame;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment