我有一个UITableView
,它比self.view
稍大。我在视图的底部有一个UITextField
,我使用代理方法– textFieldDidBeginEditing:
在文本字段开始编辑时向上移动视图。设置UITableView contentOffset,然后拖动,查看偏移量跳转到新位置
这可以正常工作,但是如果我在编辑UITextField
时尝试滚动视图(并且内容已经偏移),那么内容将跳转到视图底部“适当”的位置。换句话说,我设置的contentOffset.y
更改为等于内容视图的大小(正如您对正常行为的期望值)。
任何想法如何在编辑时重载此行为?
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// Scroll to the currently editing text field
[self scrollViewToTextField:textField];
}
- (void)scrollViewToTextField:(id)textField
{
// Set the current _scrollOffset, so we can return the user after editing
_scrollOffsetY = self.tableView.contentOffset.y;
// Get a pointer to the text field's cell
UITableViewCell *theTextFieldCell = (UITableViewCell *)[textField superview];
// Get the text fields location
CGPoint point = [theTextFieldCell convertPoint:theTextFieldCell.frame.origin toView:self.tableView];
// Scroll to cell
[self.tableView setContentOffset:CGPointMake(0, point.y - 12) animated: YES];
}