How to add UIView on top of UITableView

11,007

Solution 1

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

Solution 2

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>];
Share:
11,007
Stephen
Author by

Stephen

Updated on July 11, 2022

Comments

  • Stephen
    Stephen almost 2 years

    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?