iOS:将 UIView 添加到 UITableView
我试图在 UITableView
顶部添加一个 UIView
来模仿 iPhone Facebook 风格的菜单。我通过将控制器设置为 UIViewController 然后添加 tableview 使其工作正常,但是除非控制器是 UITableView,否则我无法将菜单设置为静态菜单。
是否可以在 tableview 上添加一个视图,并且只使后台的 tableview 可滚动,而不使前台的 view 滚动?
这是我的子类 UIViewController
但我无法通过 IB 使表格视图单元格静态,因为它不是 的子类UITableView 控制器。
根据 NSJones 代码进行编辑: 它似乎正在走上正确的轨道。然而视图仍然挡住了表。如果我从情节提要中删除视图,它将只显示表格。
I'm trying to add a UIView
on top over the UITableView
to mimic the iPhone Facebook style menu. I have it working fine by making the controller a UIViewController
then adding a tableview however I am unable to make the menu a static menu unless the controller is a UITableView
.
Is it possible to add a view ontop of a tableview and only make the tableview in the background scrollable without the view in the foreground scrolling?
Here is what I have with the subclass being UIViewController
But I am unable to make the tableview cells static via IB since it is not a subclass of UITableView
Controller.
EDIT per NSJones Code:
It seems to be going somewhat in the right track. However the view still blocks the table. If I remove the view from the storyboard it will only display the table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以像使任何真实事物悬停一样来使视图悬停;用看不见的东西举起它。
基本上,您想要做的是创建一个清晰的
UIView
(禁用用户交互),它的大小是视图控制器视图的大小,并将其作为子视图添加到视图控制器的 view 属性中。这样它就隐形地位于顶部。然后您可以将子视图添加到该清晰视图中,并且该子视图不会移动。编辑:
看来这种干净的方法不适合你,因为你需要你的视图控制器是一个
UITableViewController
。这种稍微复杂的方法的答案是使用UIScrollView
的委托方法,该方法也适用于UITableView
。 Apple 在WWDC2011 - Session 125 - UITableView Changes, Tips, Tricks
视频中对这一概念进行了精彩演示。如果你能看的话我强烈推荐它。本期的重点内容大约从36:10
开始。但总而言之,您实现了
scrollViewDidScroll:
委托方法。并通过调整视图的位置属性来处理tableview的移动。在这里,我仍然使用此方法保留名为viewToKeepStill
的UIView
属性。You can make a view hover the same way you make any real thing hover; Hold it up with something invisible.
Basically what you want to do is create a clear
UIView
(with user interaction disabled) that is the size of your view controller's view, and add it as a subview to your view controller's view property. That way it sits invisibly on top. then you can add a subview to that clear view and that subview won't move.Edit:
It seems this nice clean approach won't work for you since you need your view controller to be a
UITableViewController
. The answer for this slightly more complex approach is to use a delegate method forUIScrollView
which also works forUITableView
. Apple has a fantastic demo of this concept in theWWDC2011 - Session 125 - UITableView Changes, Tips, Tricks
video. If you can watch it I highly recommend it. The meat of this issue begins at about36:10
.But to sum it up you implement the
scrollViewDidScroll:
delegate method. And handle the movement of the tableview by adjusting the position properties of the view. Here I am keeping anUIView
property namedviewToKeepStill
still using this method.不要将其添加为表视图的子视图,而是将其添加为表视图的超级视图的子视图;这样它就不会滚动。
因此,不要这样做:
这样做:
Instead of adding it as a subview of the table view, add it as a subview of the superview of the table view; that way it won't scroll.
So instead of this:
Do this:
假设您想要在表格中始终可见的内容,请从包含菜单视图和 UITableView 的视图开始。缩小表格,使其在菜单视图开始的地方结束。表视图可以在较小的垂直空间下工作。
Assuming you want something that is visible full-time with the table, start with a view which contains both the menu view and the UITableView. Make the table smaller so it ends where the menu view begins. The table view can work with less vertical space.
如果您将 UIViewController 的视图作为表格视图,那么您的表格将跨越整个屏幕,因此您将无法在其上添加任何内容。
为什么不尝试以下操作:
仅此而已?
If you have your UIViewController's view to be your table view then your table is going to span over the whole screen, so you won't be able to add anything on top of it.
Why not try the following:
that's about it?