Skip to content

Instantly share code, notes, and snippets.

@jlcampana
Created September 12, 2012 10:12
Show Gist options
  • Save jlcampana/3705738 to your computer and use it in GitHub Desktop.
Save jlcampana/3705738 to your computer and use it in GitHub Desktop.
Scroll to a textfield on a table when tapped
//We need some properties
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITextField *currentTextField;
@property (nonatomic) CGRect originalTableFrame;
@property (nonatomic) NSIndexPath *indexForCurrentTextField;
@property (nonatomic) NSTimeInterval keyboardAnimationDuration;
//Don't forget to be delegate of UITextFieldDelegate
//Then...
#pragma mark - TextField delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (!self.currentTextField)
{
self.currentTextField = textField;
UITableViewCell *t = (UITableViewCell *)[[textField superview] superview];
self.indexForCurrentTextField = [self.tableView indexPathForCell:t];
}
else return NO;
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self endEditing:YES];
return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
// self.tableView.frame= CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, self.tableView.frame.size.height + 190);
Item *i = [self.dataSource objectAtIndex:[self.indexForCurrentTextField row]];
// do some magic...
self.currentTextField = nil;
}
#pragma mark - Keyboard
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGFloat height;
CGRect f = self.originalTableFrame;
NSValue* value = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval duration = 0;
[value getValue:&duration];
self.keyboardAnimationDuration = duration;
//Están intercambiados
if (![Misc orientationIsPortrait]) {
height = kbSize.width;
}
else
{
height = kbSize.height;
}
f.size.height-=height;
[UIView animateWithDuration:self.keyboardAnimationDuration
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
[self.tableView setFrame:f];
[self.tableView scrollToRowAtIndexPath:self.indexForCurrentTextField atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}
completion:^(BOOL finished){
}];
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
[UIView animateWithDuration:self.keyboardAnimationDuration
delay:0.0
options: UIViewAnimationCurveEaseOut
animations:^{
[self.tableView setFrame:self.originalTableFrame];
}
completion:^(BOOL finished){
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment