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:[textFie开发者_如何学Pythonld frame] animated:YES];
}
How can I avoid the keyboard hiding the textfield by bringing the textfield up? Thanks
- (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.
[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
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.
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.
}
精彩评论