BukLau
BukLau

Reputation: 69

Bringing UITextField to the top in UITableViewCell

I have UITextField in a UITableViewCell. When I click on it, the keyboard obscures it. I have tries but it does not work:

-(void) textFieldDidBeginEditing:(UITextField *)textField {
    [[self tableView] scrollRectToVisible:[textField frame] animated:YES];
}

How can I avoid the keyboard hiding the textfield by bringing the textfield up? Thanks

Upvotes: 0

Views: 755

Answers (4)

sinh99
sinh99

Reputation: 3979

- (BOOL)textViewShouldBeginEditing:(UITextView *)textField
{
CGPoint pnt = [tblView convertPoint:textField.bounds.origin fromView:textField];
path = [[tblView indexPathForRowAtPoint:pnt] retain];
[tblView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
return YES;
}

For return

 -(IBAction)Done:(id)sender
{
[tblView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[tvDetail resignFirstResponder];

}

You have to set content inset of tableviw set to 200 in tableview property in xib.

Upvotes: 1

Legolas
Legolas

Reputation: 12335

I have solved this problem (using a different logic)- With this, the screen should move up when the Keyboard shows up.

Implement this.

-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
return 40; // or choose a number based on your cell height. 
}

Upvotes: 0

ZhangChn
ZhangChn

Reputation: 3184

As what babbidi suggested, scrollToRowAtIndexPath:atScrollPosition:animated: is a good point. Also, you may change the frame of your tableView or its super view simultaneously. You can register to default notification center with UIKeyboardWillHideNotification and UIKeyboardWillShowNotification, and arrange your animation accordingly.

Upvotes: 0

alex-i
alex-i

Reputation: 5454

[textField frame] contains the location of the textfield inside the table cell, not inside the table itself. You need a way to compute the area of where the textField is in the table, or it might be better to use scrollToRowAtIndexPath:atScrollPosition:animated: instead. (when setting up the table cells you can set its textField.tag=indexPath.row to make this easier).

I hope it helps

Upvotes: 0

Related Questions