Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 542
Stephen Avatar asked Dec 02 '22 14:12

Stephen


2 Answers

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

like image 121
Jogendra.Com Avatar answered Jan 01 '23 22:01

Jogendra.Com


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>];
like image 35
Greg Avatar answered Jan 02 '23 00:01

Greg