Is it possible to get the height of the section header of a table dynamic view using automatic layout?

New in iOS 8, you can get 100% dynamic table table cells by simply setting the approximate row height, and then place your items in the cell using the automatic layout. If the content increases in height, the cell will also increase in height. This is very useful, and I wonder if it is possible to do the same feat for section headings in a table form?

You can create, for example, a UIView in tableView:viewForHeaderInSection: add UILabel subtext, set layout restrictions for the label for the view, and increase the height of the view to fit the contents of the label, without having to execute tableView:heightForHeaderInSection: :?

The documentation for viewForHeaderInSection states: "This method only works correctly when tableView: heightForHeaderInSection: is also implemented." I have not heard if something has changed for iOS 8.

If you cannot do this, what is the best way to simulate this behavior?

+93
ios uitableview autolayout ios8
Apr 05 '15 at 21:17
source share
8 answers

It is possible. This is a new right along with dynamic cell heights implemented in iOS 8.

It is very simple. Just add this to viewDidLoad :

 self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension; self.tableView.estimatedSectionHeaderHeight = 25; 

Then override viewForHeaderInSection and use auto-layout to limit the elements in your view as seen. It! There is no need to implement heightForHeaderInSection . And actually sectionHeaderHeight also does not need to be specified, I just added it for clarity.

Please note that in iOS 11, cells and header / footer views use estimated default heights. The only thing that needs to be done is to give a height estimate in order to better inform the system of what to expect. The default value is UITableViewAutomaticDimension , but you should provide a more accurate estimate, which is average height, if possible.

+239
Apr 21 '15 at 4:53 on
source share

This can be achieved by setting (or returning) the estimatedSectionHeaderHeight in a table form.

If the section header overlaps your cells after setting estimatedSectionHeaderHeight , make sure you use estimatedRowHeight .

(I add this answer because the second paragraph contains the answer to the question, which can be found after reading all the comments that some may miss.)

+22
May 14 '16 at 21:10
source share

I tried

 self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension; self.tableView.estimatedSectionHeaderHeight = 25; 

but he did not correctly order the multi-line label header. Added this to solve my problem:

 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Recalculates height tableView.beginUpdates() tableView.endUpdates() } 
+6
Sep 13 '17 at 0:35
source share

Stuck in the same problem where the header was getting zero height until I provide a fixed height in heighForHeaderInSection for heighForHeaderInSection .

I tried many solutions, which includes

 self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension; self.tableView.estimatedSectionHeaderHeight = 73' 

But nothing worked. My camera also used the correct auto-scaling. The lines changed their height dynamically using the following code, but the section title was not.

 self.tableView.estimatedRowHeight = 135 self.tableView.rowHeight = UITableViewAutomaticDimension' 

The fix is ​​very simple and strange, but I had to implement delegate methods instead of the 1-line code for the estimatedSectionHeaderHeight sectionHeaderHeight and sectionHeaderHeight which in my case looks like this.

 func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return UITableViewAutomaticDimension } func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat { return 73 } 
+4
Feb 14 '19 at 7:07
source share

In my case:

  1. install software, do not work .

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension .

  1. set the storyboard to not work .

  2. override heightForHeaderInSection worked .

 override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return UITableViewAutomaticDimension } 

test environment:

  • Mac OS 10.13.4
  • Xcode Version 9.4.1
  • Simulator iPhone 8 Plus
+1
03 Oct '18 at 7:13
source share

Yes, it works for me. I have to make more changes as shown below:

 override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let label = UILabel() label.numberOfLines = 0 label.text = my own text return label } 
0
Sep 28 '18 at 15:42
source share

Swift 4+

works (100% verified)

If you need both a section and a line with a dynamic height based on the content, you can use the following code:

On viewDidLoad () write these lines:

  self.globalTableView.estimatedRowHeight = 20 self.globalTableView.rowHeight = UITableView.automaticDimension self.globalTableView.sectionHeaderHeight = UITableView.automaticDimension self.globalTableView.estimatedSectionHeaderHeight = 25; 

Now we set the row height and section height using the UITableView delegate methods:

 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableView.automaticDimension } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return UITableView.automaticDimension } 
0
May 24 '19 at 2:59
source share

I modified the response uriimosis . Just replaced the viewWillAppear method:

 tableView.sectionHeaderHeight = UITableViewAutomaticDimension tableView.estimatedSectionHeaderHeight = 25 override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) // Recalculates height tableView.layoutIfNeeded() } 

Also add tableView.layoutIfNeeded () to

 override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) tableView.layoutIfNeeded() } 

For iOS 10

 tableView.beginUpdates() tableView.endUpdates() 

have a fade effect on viewWillAppear for me

-one
Apr 05 '18 at 13:46
source share



All Articles