asheyla
asheyla

Reputation: 3325

Swift set height of table

I have this table for which I want a certain height even when I only have one row. I want to make it scrollable (empty space at the bottom) even when I do not have enough rows to cover the whole screen.

I think I should work with:

tableView.contentSize.height = 2000

But either I do not know where I should put it(should I reloadData for the table after?) or I have the wrong idea of how to do this.

Can anybody help?

EDIT: I have this view inside the table view which in some cases might be higher than the table and in this case I can not scroll it all. This is why I need to change the table in scroll height.

On iOS 10 it works as intended, but I have to fix it for iOS 8 and 9 also.

Upvotes: 1

Views: 8952

Answers (3)

asheyla
asheyla

Reputation: 3325

Only at this hour at night have I realised what works. I can't set the height of the content size directly, but like this:

tableView.reloadData()
tableView.layoutIfNeeded()

var contentSizeTemp = tableView.contentSize
contentSizeTemp.height = menuViewHeight.constant 
tableView.contentSize = contentSizeTemp

Upvotes: 1

random
random

Reputation: 8608

This has been tested and works:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    tableView.contentSize.height = 2000
}

Example: enter image description here

Upvotes: 4

Maksym Musiienko
Maksym Musiienko

Reputation: 1248

What about setting constraints for table view? Or it's another case when they are not suitable? In this case table will have height which is equal screen height no matter how much rows it has. enter image description here

Upvotes: 0

Related Questions