简体   繁体   中英

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.

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:

在此输入图像描述

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

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

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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