Created
August 28, 2013 12:43
-
-
Save tudormunteanu/6365593 to your computer and use it in GitHub Desktop.
FormKit auto-focus for non-UITableViewControllers
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
- (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