Pablo
Pablo

Reputation: 29519

Add top/bottom views to UITableViewController?

In my storyboard I have UITableViewController which has custom class. How can I add UIView objects on top and bottom parts of the table view?

Do I have to create UITableView and two desired views inside of UIView and then manually init my controller, set tableView or there is a better way?

Upvotes: 1

Views: 3563

Answers (3)

Yes it's possible in UITableViewConroller:

    let bottomView = UIView()
    bottomView.backgroundColor = .red // or your color
    bottomView.frame = CGRect(x: 0, y: UIScreen.main.bounds.size.height - 78, width: tableView.frame.size.width, height: 78) // 78 or your size of view
    navigationController?.view.addSubview(bottomView)
    tableView.tableFooterView = UIView()

enter image description here

Upvotes: 1

David Berry
David Berry

Reputation: 41236

If you're wanting to use UITableViewController for it's abilities to manage cells and static layouts, the best possibility would be to use a container view in your storyboard. This would allow you to layout the main view contents around the table and still keep a separate UITableViewController maintaining the table itself. Set up the right auto layout constraints (really simple, just anchor the container view to the header and footer views)

enter image description here

Upvotes: 3

klcjr89
klcjr89

Reputation: 5902

Something like this should work for you:

// Your frame/view will vary
UIView *subview = [[UIView alloc]initWithFrame:CGRectMake(0, 64, 320, 44)];
subview.backgroundColor = [UIColor redColor];

[self.navigationController.view addSubview:subview];

Upvotes: -2

Related Questions