1

I added a UIView above a tableview to make it scrollable and have some tableviewcell below it but I can't figure out how to set "Detail Header" view's height dynamic so it fits to my content. I have a textLabel inside which increases its height because of this

enter image description here

All constraints inside the View are fine but somehow that UIView doesn't adjust to content and it keeps its fixed size

override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    return detailHeader
}
Charlie Fish
  • 18,491
  • 19
  • 86
  • 179

2 Answers2

0

As per my understanding of your question - how to set a table view header height dynamically -

  • at viewDidLoad method, please use below lines:

    tableView.estimatedSectionHeaderHeight = 120

    //open var sectionHeaderHeight: CGFloat(default is UITableViewAutomaticDimension)

    tableView.sectionHeaderHeight = UITableViewAutomaticDimension

  • override viewForHeaderInSection and use Auto Layout to constrain elements in your view as seen fit. That's done.

"doesn't allow me to put images yet" what you mean? As per my understanding if you are using Storyboard and Drag and Drop Image so it will assign an image straight forward using image option on the right pane.

Vinay Mishra
  • 386
  • 2
  • 15
  • **We're close to fix this! Thank you so much**, now there's some space in the cells, I don't know why https://i.imgur.com/7xgWqd2.png – Héctor Gómez Jul 06 '19 at 06:33
  • As per the image, please check the row height and header height, what currently you are set. Maybe you are not adding auto layout or stackview Height and Width corresponds to superview. – Vinay Mishra Jul 06 '19 at 06:55
0

This is late... but I had this exact problem with setting the height of a regular UIView above table view cells inside of a Table View.

I solved it by creating a reference to this top UIView in the view controller holding the table view. Then I just set the height to it dynamically based on the my criteria.

Example code:

topView.frame.size.height = 50

tableView.layoutIfNeeded()

Andrew
  • 619
  • 1
  • 7
  • 10