iOS:将 UIView 添加到 UITableView

发布于 2025-01-05 18:26:13 字数 626 浏览 1 评论 0原文

我试图在 UITableView 顶部添加一个 UIView 来模仿 iPhone Facebook 风格的菜单。我通过将控制器设置为 UIViewController 然后添加 tableview 使其工作正常,但是除非控制器是 UITableView,否则我无法将菜单设置为静态菜单。

是否可以在 tableview 上添加一个视图,并且只使后台的 tableview 可滚动,而不使前台的 view 滚动?

这是我的子类 UIViewController 在此处输入图像描述

但我无法通过 IB 使表格视图单元格静态,因为它不是 的子类UITableView 控制器。

根据 NSJones 代码进行编辑: 它似乎正在走上正确的轨道。然而视图仍然挡住了表。如果我从情节提要中删除视图,它将只显示表格。 xCode 截图

iPhone 模拟器截图

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
enter image description here

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.
xCode screenshot

iPhone Simulator Screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

饮惑 2025-01-12 18:26:13

您可以像使任何真实事物悬停一样来使视图悬停;用看不见的东西举起它。

基本上,您想要做的是创建一个清晰的 UIView (禁用用户交互),它的大小是视图控制器视图的大小,并将其作为子视图添加到视图控制器的 view 属性中。这样它就隐形地位于顶部。然后您可以将子视图添加到该清晰视图中,并且该子视图不会移动。

编辑:

看来这种干净的方法不适合你,因为你需要你的视图控制器是一个UITableViewController。这种稍微复杂的方法的答案是使用 UIScrollView 的委托方法,该方法也适用于 UITableView。 Apple 在WWDC2011 - Session 125 - UITableView Changes, Tips, Tricks 视频中对这一概念进行了精彩演示。如果你能看的话我强烈推荐它。本期的重点内容大约从 36:10 开始。

但总而言之,您实现了 scrollViewDidScroll: 委托方法。并通过调整视图的位置属性来处理tableview的移动。在这里,我仍然使用此方法保留名为 viewToKeepStillUIView 属性。

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // CGFloat stillViewDesiredOriginY; declared ivar
    CGRect tableBounds = self.tableView.bounds; // gets content offset
    CGRect frameForStillView = self.viewToKeepStill.frame; 
    frameForStillView.origin.y = tableBounds.origin.y + stillViewDesiredOriginY; // offsets the rects y origin by the content offset
    self.viewToKeepStill.frame = frameForStillView; // set the frame to the new calculation
}

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 for UIScrollView which also works for UITableView. Apple has a fantastic demo of this concept in the WWDC2011 - Session 125 - UITableView Changes, Tips, Tricks video. If you can watch it I highly recommend it. The meat of this issue begins at about 36: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 an UIView property named viewToKeepStill still using this method.

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // CGFloat stillViewDesiredOriginY; declared ivar
    CGRect tableBounds = self.tableView.bounds; // gets content offset
    CGRect frameForStillView = self.viewToKeepStill.frame; 
    frameForStillView.origin.y = tableBounds.origin.y + stillViewDesiredOriginY; // offsets the rects y origin by the content offset
    self.viewToKeepStill.frame = frameForStillView; // set the frame to the new calculation
}
掩于岁月 2025-01-12 18:26:13

不要将其添加为表视图的子视图,而是将其添加为表视图的超级视图的子视图;这样它就不会滚动。

因此,不要这样做:

[tableView addSubview:viewController.view];

这样做:

[tableView.superview addSubview:viewController.view];

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:

[tableView addSubview:viewController.view];

Do this:

[tableView.superview addSubview:viewController.view];
再见回来 2025-01-12 18:26:13

假设您想要在表格中始终可见的内容,请从包含菜单视图和 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.

难如初 2025-01-12 18:26:13

如果您将 UIViewController 的视图作为表格视图,那么您的表格将跨越整个屏幕,因此您将无法在其上添加任何内容。

为什么不尝试以下操作:

1) create a new UIViewController
2) add a view on top where you want your menu
3) in the space left under just drag a table view from the component library 
4) don't forget to set the 2 table view delegates to be your view controller class

仅此而已?

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:

1) create a new UIViewController
2) add a view on top where you want your menu
3) in the space left under just drag a table view from the component library 
4) don't forget to set the 2 table view delegates to be your view controller class

that's about it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文