Add a UIView over the top of UITableViewController

2019-02-18 03:14发布

I currently use a UITableViewController (PFQueryTableViewController), I would like to display a UIView over the top of the TableView.

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 :

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.

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:

enter image description here

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.

Can anyone help me create this look? Thanks in advance

1条回答
劳资没心,怎么记你
2楼-- · 2019-02-18 03:52

You have to derive from UIViewController to get full layout control.

Then simply add a UITableView instance in Storyboard. Constrain it normally, edge-flush, top-flush, and have 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()).

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.

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. Just use the right tool. The top-level view property of a UITableViewController is the UITableView itself. See below, a stock UITableViewController with nothing else, zero code, in layout debug mode.

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. After all, your rendering code in cellForRowAtIndexPath and your dataSource and delegate generally, don't change at all.

ADDENDUM 2:

Not knowing Parse intimately, this may not be possible. (!! 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:

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

enter image description here

查看更多
登录 后发表回答