0
我有一个视图,顶部有一个工具栏,在其单元格中有一个带有文本字段的表格视图。当我想要编辑位于表格视图底部的文本字段时,键盘隐藏了正在编辑的文本字段(因为表格视图没有向上滚动)。 我试图实现http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html,但这使得我的整个视图向上移动(工具栏和表格视图),同时我想一直在视图顶部放置toobar。 我修改所提到的代码是这样的:如何使用工具栏在视图顶部滚动表格视图
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CGRect textFieldRect = [tableView.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [tableView.window convertRect:tmpTableView.bounds fromView:tableView];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator/denominator;
if (heightFraction < 0.0) {
heightFraction = 0.0;
} else if (heightFraction > 1.0) {
heightFraction = 1.0;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
} else {
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = tableView.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[tableView setFrame:viewFrame];
[UIView commitAnimations];
}
和
- (void)textFieldDidEndEditing:(UITextField *)textField {
CGRect viewFrame = tableView.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[tableView setFrame:viewFrame];
[UIView commitAnimations];
}
这在顶部做我的工具栏逗留,但不幸的是,表视图覆盖的工具栏和工具栏不可见。
任何想法如何解决这个问题?
但我想要滚动表格视图以在键盘上方编辑文本字段。在简单调整表格视图的大小的情况下,sam行为是否可行? – Jakub 2010-04-06 19:34:45
调整大小后,您可以使用'scrollToRowAtIndexPath:atScrollPosition:animated:'滚动任何单元格到可见区域。 – 2010-04-06 20:24:13
如果我可以将任何单元格滚动到可见区域,那么调整大小的用途是什么?即使表格视图是正常大小,我也可以sc right,对吧? btw。我无法制作 scrollToRowAtIndexPath:atScrollPosition:animated:work。我试过了: NSIndexPath * ip = [NSIndexPath indexPathForRow:1 inSection:1]; [self.settingsTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO]; 从第二行滚动第二个单元格(只是为了查看它是否有效),它隐藏在键盘下。 – Jakub 2010-04-07 07:37:34