Labinot Bajrami
Labinot Bajrami

Reputation: 3

How to make uitableview to fill all my view?

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

Check Image

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

Upvotes: 0

Views: 916

Answers (2)

fluidsonic
fluidsonic

Reputation: 4676

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:).

Upvotes: 1

gnasher729
gnasher729

Reputation: 52530

To fill the whole view, with a table view, I use four constraints:

One. tableView centre = view centre. Two. tableView width = view width. Three. tableView vertical distance to top layout guide = 0 Four. tableView vertical distance to bottom layout guide = 0.

To fill everything with the same color, I set the backgroundColor of the tableView, then set the backgroundColor of the cells to [UIColor clearColor].

Upvotes: 0

Related Questions