iOS:将UIView添加到UITableView [英] iOS: Add UIView to UITableView

查看:90
本文介绍了iOS:将UIView添加到UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 UITableView 的顶部添加 UIView 来模仿iPhone Facebook样式菜单。通过使控制器成为 UIViewController 然后添加一个tableview,我可以正常工作但是我无法使菜单成为静态菜单,除非控制器是 UITableView

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.

是否可以在桌面视图的顶部添加视图,只能使背景中的tableview可滚动而不在前景中查看滚动?

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?

以下是我的子类 UIViewController

Here is what I have with the subclass being UIViewController

但是我无法将tableview单元格设为静态IB因为它不是 UITableView Controller的子类。

But I am unable to make the tableview cells static via IB since it is not a subclass of UITableView Controller.

每个NSJones编码代码:
似乎走在正确的轨道上。但是视图仍会阻止该表。如果我从故事板中删除视图,它将只显示该表。

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.

推荐答案

您可以使视图悬停,就像您将任何真实的东西悬停一样;坚持不可见的东西。

You can make a view hover the same way you make any real thing hover; Hold it up with something invisible.

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

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.

编辑:

看起来这个很好的干净方法对你不起作用,因为你需要你的视图控制器是一个 UITableViewController 。这个稍微复杂的方法的答案是使用 UIScrollView 的委托方法,这也适用于 UITableView 。 Apple在 WWDC2011 - Session 125 - UITableView Changes,Tips,Tricks 视频中有这个概念的精彩演示。如果你能看到它我强烈推荐它。这个问题的主要内容大约是 36:10

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.

但总结一下,你实现了 scrollViewDidScroll:委托方法。并通过调整视图的位置属性来处理tableview的移动。在这里,我保持一个名为 viewToKeepStill UIView 属性仍在使用此方法。

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
}

这篇关于iOS:将UIView添加到UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆