swiftuitableviewheightswift3contentsize

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.


Solution

  • 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