简体   繁体   English

如何使用视图顶部的工具栏滚动表视图

[英]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.

相关问题 我们如何知道iPhone中的表格视图中的滚动到顶部(按下顶部) - How we know the Scroll to top ( push to top) in table view in iPhone 启用滚动视图滚动到顶部 - enabling scroll view scroll to top 如何在iPhone应用程序的表视图中添加工具栏? - How to add toolbar to table view in iphone apps? 如何在表视图顶部正确堆叠容器视图 - How to properly stack a container view on top of a table view 如何创建一个视图,底部有一个选择器,顶部是一个表格视图? - How do I create a view with a picker on the bottom and a table view on the top? iPhone:将表格视图单元格滚动到可见的自定义键盘对齐工具栏上方? - iPhone: scroll table view cell to visible above custom keyboard-aligned toolbar? 如何防止滚动 UItable 视图的 tableHeaderView,粘在顶部 - How to prevent scroll the tableHeaderView of UItable view, To stick at the top 在另一个ui滚动视图的顶部添加分页的ui滚动视图 - Adding a paged ui scroll view on top of another ui scroll view 移动表格视图并保持工具栏可见? - Moving the table view and keep the toolbar visible? 如何在滚动视图中突出显示焦点的UITextField时将其移动到键盘焦点/视图中心(位于焦点上)的顶部 - How to highlight the focussed UITextField in a scroll view by moving it to the top of the key board / center of the view when it was focussed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM