- There is no parent view before headView, and there is no need to set frame
- Make the layout of the subview in the headView can give him a certain height
- Then call the following code and it's ok (my own test is valid)
let size = headView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
let rect = CGRect(x: 0, y: 0, width: 0, height: size.height)
headView.frame = rect
tableView.tableHeaderView = contentView
Reference blog:
tableHeaderView adaptive height - short book