简体   繁体   English

在Swift中的UITableViewController顶部添加一个UIView

[英]Add a UIView over the top of UITableViewController in Swift

I currently use a UITableViewController (PFQueryTableViewController), I would like to display a UIView over the top of the TableView. 我目前使用的是UITableViewController(PFQueryTableViewController),我想在TableView的顶部显示一个UIView。

Ideally I'd like to do this in the storyboard, so I can easily add additional Labels/buttons to it. 理想情况下,我想在故事板中执行此操作,因此我可以轻松地为其添加其他标签/按钮。 However, I can't seem to figure out how or if you can do it in storyboard. 但是,我似乎无法弄清楚如何或如果你能在故事板中做到这一点。 Is that right? 是对的吗?

I have tried it programmatically. 我以编程方式尝试过它。 I first created the variable at the top: 我首先在顶部创建变量:

var filterLabelView:UIView = UIView()

I then added the following in ViewDidLoad : 然后我在ViewDidLoad添加了以下ViewDidLoad

filterLabelView.frame = CGRect(x: self.view.frame.width/2, y: self.view.frame.height/2, width: self.view.frame.width, height: 40)
filterLabelView.center = CGPointMake(self.view.frame.size.width  / 2, self.view.frame.size.height / 2)
filterLabelView.backgroundColor = UIColor.redColor()
self.view.addSubview(filterLabelView) // See below

I also tried: 我也尝试过:

self.view.insertSubview(filterLabelView, aboveSubview: tableView)

This creates the red UIView, but it seems to be embedded to the tableview, as when I scroll up and down, the View moves with it. 这会创建红色UIView,但它似乎嵌入到tableview中,因为当我向上和向下滚动时,View会随之移动。

What I want to create 我想要创造什么

Ideally, I want the view to sit at the bottom of the screen, and not move when the user scrolls. 理想情况下,我希望视图位于屏幕的底部,而不是在用户滚动时移动。 An example screenshot of how I want it look is below: 我想要它的样子截图如下:

在此输入图像描述

I have read that the best way is to use a UIViewController with a UITableView inside it, but I'd rather not take that approach seeing how much my app is already built up. 我已经读过,最好的方法是在其中使用带有UITableView的UIViewController,但我不想采用这种方法来查看我的应用程序已经构建了多少。

Can anyone help me create this look? 任何人都可以帮我创造这种外观吗? Thanks in advance 提前致谢

You have to derive from UIViewController to get full layout control. 您必须从UIViewController派生才能获得完整的布局控制。

Then simply add a UITableView instance in Storyboard. 然后只需在Storyboard中添加一个UITableView实例。 Constrain it normally, edge-flush, top-flush, and have customView.top = tableView.bottom . 正常约束,边缘刷新,顶部刷新,并具有customView.top = tableView.bottom Make a normal outlet to your controller. 为控制器提供正常插座。

You just need to remember to make your custom UIViewController adopt the usual dataSource and delegate protocols, as well as assigning itself as those roles to the properties of the UITableView on initialization (usually viewDidLoad() ). 您只需要记住使自定义UIViewController采用通常的dataSource和委托协议,以及在初始化时将自己指定为UITableView属性的角色(通​​常是viewDidLoad() )。

There's one more finesse related to clearing the selected cell when on viewDidAppear() , but there's nothing else special about a UITableViewController -- it's just a UIViewController with a built-in tableView property and automatically assigned delegates and a very inflexible layout. viewDidAppear()上还有一个与清除所选单元格有关的技巧,但是UITableViewController没有别的特别之处 - 它只是一个带有内置tableView属性的UIViewController ,并且自动分配了委托和一个非常不灵活的布局。

ADDENDUM based on comment about how to get around this: It's sort of like asking if there is any way to make a screwdriver drive a nail into wood. ADDENDUM基于关于如何解决这个问题的评论:这有点像询问是否有任何方法可以使螺丝刀将钉子钉入木材中。 Just use the right tool. 只需使用正确的工具。 The top-level view property of a UITableViewController is the UITableView itself. UITableViewController的顶级view属性是UITableView本身。 See below, a stock UITableViewController with nothing else, zero code, in layout debug mode. 在布局调试模式下,请参见下面的库存UITableViewController ,其他内容为零,零代码。

This means the entire tree of views is embedded in a scrolled entity, so all subviews will scroll with it. 这意味着整个视图树嵌入在滚动实体中,因此所有子视图都将随之滚动。

It really shouldn't be that big a deal to re-do this one VC in Storyboard. 在故事板中重新做这个VC真的不应该是一件大事。 After all, your rendering code in cellForRowAtIndexPath and your dataSource and delegate generally, don't change at all. 毕竟,你在cellForRowAtIndexPath和你的dataSource和委托中的渲染代码一般都不会改变。

ADDENDUM 2: 附录2:

Not knowing Parse intimately, this may not be possible. 不熟悉Parse,这可能是不可能的。 (!! Thanks, Parse.) If Parse doesn't supply you with a separable PFQueryTableView that you can embed yourself, the following workaround may be possible, but you'll need to understand the Objective-C code: (!!谢谢,Parse。)如果Parse没有为您提供可以自己嵌入的可分离PFQueryTableView ,则可能采用以下解决方法,但您需要了解Objective-C代码:

https://www.parse.com/questions/pfquery-tableview-for-uiviewcontroller https://www.parse.com/questions/pfquery-tableview-for-uiviewcontroller

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM