How to render uitableview to fill all my views?

advertisements

I am very new to iOS. Can anyone tell me how to fix this, to fill all my view with UITableView:

I am trying to do it with auto layout and constraints but I don't know why isn't working.


What you want to do is to have the cells at equal height so that all cells together fit exactly the height of the screen.

You cannot do that with auto layout. You have to tell the table view the height you'd like the rows to have like so:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    let heightOfVisibleTableViewArea = view.bounds.height - topLayoutGuide.length - bottomLayoutGuide.length
    let numberOfRows = tableView.numberOfRowsInSection(0)

    tableView.rowHeight = heightOfVisibleTableViewArea / CGFloat(numberOfRows)
}

Note that this code assumes that you don't implement the method tableView(_:heightForRowAtIndexPath:).