Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FormKit auto-focus for non-UITableViewControllers
- (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
You can’t perform that action at this time.