Stephen
Stephen

Reputation: 3465

How to add UIView on top of UITableView

I have a table in my viewController class

On tapping of a button (this button is outside of table), i wanted to show some UIView on top of tableView alone and a web service request is sent. After receiving the response, i want to remove the UIView which i have added.

tableView.addSubview(myview) didn't work for me, but self.view.addSubview(myview) worked but i wanted to overlay my UIView only on top of table view.

My question is specific to how to add/remove subview to a tableview. How can i achieve this?

Upvotes: 4

Views: 12726

Answers (2)

Jogendra.Com
Jogendra.Com

Reputation: 6454

Try like this for adding header on top

let headerView = UIView(frame: CGRect(x: XXX, y: YYY, width: XXX, height: YYY))
let imageView = UIImageView(frame: CGRect(x: XXX, y: YYY, width: XXX, height: YYY))
headerView.addSubview(imageView)
let labelView = UILabel(frame: CGRect(x: XXX, y: YYY, width: XXX, height: YYY))
headerView.addSubview(labelView)
self.tableView.tableHeaderView = headerView

It'll add header on UITableView

Upvotes: 15

Greg
Greg

Reputation: 900

If you're using a UINavigationController with the table as one of it's view controllers, I have found the easiest way is to insert it here directly above the UITableView. This code could be called in your UITableViewController when you tap the button to display the view:

 [self.navigationController.view addSubview:<your UIView>];

Upvotes: 2

Related Questions