[英]Add floating view (i.e. static and fixed) to UITableViewController
I'm trying to add a custom subview to a UITableViewController
that doesn't move when the user scrolls. 我正在尝试向用户滚动时不移动的
UITableViewController
添加自定义子视图。
Is it possible to add such a view? 是否可以添加这样的视图?
I'm using a UITableViewController
. 我正在使用
UITableViewController
。 I would switch to a UIViewController
and add a UITableView
, but the code relies on the UITableViewController's refreshControl
. 我会切换到
UIViewController
并添加一个UITableView
,但代码依赖于UITableViewController的refreshControl
。 Adding a container view and having two controllers seems a bit much for such a simple task! 添加一个容器视图并拥有两个控制器对于这么简单的任务来说似乎有点多了!
I've also tried adding the content to the UINavigationController
view, but unfortunately it doesn't animate smoothly when the view controller appears and disappears. 我也尝试将内容添加到
UINavigationController
视图中,但不幸的是,当视图控制器出现并消失时,它没有平滑的动画。
Is there any way to add a fixed subview to a UITableView
? 有没有办法将固定的子视图添加到
UITableView
?
Edit: 编辑:
Adding a container view and having two controllers (with an embed segue) is difficult for this project, since I'm updating an older code base and there is a lot of legacy code that depends on the main controller being a UITableViewController
. 添加容器视图并具有两个控制器(带有嵌入segue)对于这个项目来说很难,因为我正在更新旧的代码库,并且有很多遗留代码依赖于主控制器是
UITableViewController
。 Is there any way to achieve this without an embed segue / two view controllers? 有没有任何方法可以实现这个没有嵌入segue /两个视图控制器?
Unfortunately creating a container view for a UITableViewController
inside of a UIViewController
isn't feasible in this case. 不幸的是,在这种情况下,为
UIViewController
内部的UITableViewController
创建容器视图是不可行的。 That is a great solution if you're early enough in development to arrange things properly . 如果您在开发过程中尽早安排正确的事情,这是一个很好的解决方案 。
The best solution I've found, is to transform the floating view's y
position on scrollViewDidScroll
as per this answer : 我发现的最佳解决方案是根据以下答案在
scrollViewDidScroll
上转换浮动视图的y
位置:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
self.header.transform = CGAffineTransformMakeTranslation(0, self.tableView.contentOffset.y);
}
I have seen other answers which modify the floating view's frame, but I think transforming is probably more appropriate. 我已经看到了修改浮动视图框架的其他答案,但我认为转换可能更合适。
As an interesting aside, I was able to create a floating view that 'dismisses' when the user scrolls down, but 'floats' when the user pulls to refresh by checking the table view offset before applying the transform. 有趣的是,我能够创建一个浮动视图,当用户向下滚动时“解散”,但是当用户通过在应用转换之前检查表视图偏移来拉动刷新时“浮动”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.