New in iOS 8, you can obtain 100% dynamic table view cells by simply setting the estimated row height, then layout your elements in the cell using Auto Layout. If the conten
Yes, it works for me. I have to make more changes as below:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let label = UILabel()
label.numberOfLines = 0
label.text = my own text
return label
}
In my case:
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension
.
set in storyboard not work.
override heightForHeaderInSection
worked.
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return UITableViewAutomaticDimension }
test enviroment:
I modified iuriimoz answer. Just replaced viewWillAppear method:
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
tableView.estimatedSectionHeaderHeight = 25
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Recalculates height
tableView.layoutIfNeeded()
}
Also add the tableView.layoutIfNeeded() to
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
tableView.layoutIfNeeded()
}
For iOS 10
tableView.beginUpdates()
tableView.endUpdates()
have "fade" animation effect on viewWillAppear for me
Got stuck in the same issue where header was getting zero height untill and unless I provide a fixed height in the delegate for heighForHeaderInSection
.
Tried a lot of solutions which includes
self.tableView.sectionHeaderHeight = UITableView.automaticDimension
self.tableView.estimatedSectionHeaderHeight = 73
But nothing worked. My cell were using proper autolayouts too. Rows were changing their height dynamically by using the following code but section header weren't.
self.tableView.estimatedRowHeight = 135
self.tableView.rowHeight = UITableView.automaticDimension
The fix is extremely simple and weird too but I had to implement the delegate methods instead of 1 line code for the estimatedSectionHeaderHeight
and sectionHeaderHeight
which goes as follows for my case.
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
return 73
}
This is possible. It is new right alongside the dynamic cell heights implemented in iOS 8.
It's very simple. Just add this in viewDidLoad
:
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 25;
Then override viewForHeaderInSection
and use Auto Layout to constrain elements in your view as seen fit. That's it! No need to implement heightForHeaderInSection
. And actually the sectionHeaderHeight
doesn't need not be stated either, I just added it for clarity.
Note that in iOS 11, cells and header/footer views use estimated heights by default. The only thing to do is provide an estimated height to better inform the system what to expect. The default value is UITableViewAutomaticDimension
but you should provide a better estimate that is the average height they will be if possible.
I tried
self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 25;
but it didn't size correctly header with multiline label. Added this to solve my problem:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Recalculates height
tableView.beginUpdates()
tableView.endUpdates()
}