[英]How to scroll table view with toolbar at the top of the view
I have a view with a toolbar at the top and a table view with text fields in it's cells. 我有一个顶部带有工具栏的视图,一个表格视图带有单元格中的文本字段。 When I want to edit the text fields placed in the bottom of the table view the keyboard is hiding the text field being edited (as the table view is not scrolled up).
当我要编辑放置在表格视图底部的文本字段时,键盘隐藏了正在编辑的文本字段(因为表格视图没有向上滚动)。 I tried to implement http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html but this makes my whole view move up (the toolbar and the table view) while I would like to have the toobar at the top of the view for the whole time.
我尝试实现http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html,但这使我的整个视图(工具栏和表格视图)向上移动,而我希望整个时间都位于视图顶部。 I modified the mentioned code to something like this:
我将上述代码修改为如下形式:
- (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];
}
and 和
- (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];
}
This made my toolbar stay at the top, but unfortunately the table view overlays the toolbar and the toolbar is not visible. 这使我的工具栏停留在顶部,但是不幸的是,表格视图覆盖了该工具栏,并且该工具栏不可见。
Any ideas how to solve this? 任何想法如何解决这个问题?
与其重新放置表格视图,不如重新调整表格视图的大小,以使键盘不与之重叠。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.