I have a split view controller: view and tableview. In this table view, I have custom cells with a text box. I donβt know how many cells I will have, so it will be generated automatically. And now I'm trying to scroll the text box when it becomes FirstResponder. I tried something like this:
-(void) textFieldDidBeginEditing:(UITextField *)textField { CGPoint focusOnTextField = CGPointMake(0, 300 + textField.frame.origin.y); [scroller setContentOffset: focusOnTextField animated: YES]; }
300px is the starting position of my TableView. Everything looks good, but textField.frame.origin.y always 0 (for example, and bounds.origin.y btw).
I thought I could solve the problem if I get the position of the cell whose text field is active, and then replace textField.frame.origin.y with cell.frame.origin.y or something like that.
==================================================== ==================
I forgot to say that my scroll table is disabled. I follow your tips and code examples and solve them like this:
- (UITableViewCell *)cellWithSubview:(UIView *)subview { while (subview && ![subview isKindOfClass:[UITableViewCell self]]) subview = subview.superview; return (UITableViewCell *)subview; } - (void)textFieldDidBeginEditing:(UITextField *)textField { UITableViewCell *activeCell = [self cellWithSubview:textField]; float offsetValueY = 200 + activeCell.origin.y; CGPoint focusOnTextField = CGPointMake(0, offsetValueY); [scroller setContentOffset:focusOnTextField animated:YES]; }
And you know what? It works! :-) But this creates a new problem. When I start editing a text field, the scroller first jumps from above, and only then comes to the right position. When I write [scroller setContentOffset:focusOnTextField animated:NO]; , and this problem disappears, but there is no smooth movement of the scroller. And this is bad for me :-) So how can we solve this?
source share