How to add UIView on top of UITableView

Stephen picture Stephen · Jun 27, 2016 · Viewed 10.2k times · Source

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?

Answer

Jogendra.Com picture Jogendra.Com · Jun 27, 2016

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