0

I have a headerview in my UITableView but I can't find a solution to make it dynamically height. In my headerview I have a UITextView which height depends on its content.

What I have tried so far.

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    sizeHeaderToFit()
}


func sizeHeaderToFit() {

    let headerView = mTabView.tableHeaderView!

    mTxtViewDescription.sizeToFit()
    mConDescriptionHeight.constant = mTxtViewDescription.frame.height

    print(mContainerView.frame.maxY)

    headerView.frame.size.height += mTxtViewDescription.frame.height


}

The green area is my headerview

enter image description here

Loc Dai Le
  • 1,661
  • 4
  • 35
  • 70

1 Answers1

0

Changing the height of the frame of a UITableView header that's already been set won't recalculate the height, meaning you can't just change the height of a tableHeaderView as its layout is managed by the tableView. You need to set the tableHeaderView again, setting the property triggers the new calculation. There are a number of questions going into detail about this, several linked from this one.

Community
  • 1
  • 1
Mike Sand
  • 2,750
  • 14
  • 23