Returning CGFloat.leastNormalMagnitude for the UITableView section header causes a crash

I made an iOS 8 app that uses a grouped UITableView for one of its pages. It has several sections that use CGFloat.leastNormalMagnitude (or CGFloat.min in Swift 2 and below) for section height and footer height to remove the "default space". Everything went well until the application started in iOS 9 and 10, where it crashes with this error:

The application terminated due to the uncaught exception "NSInternalInconsistencyException", reason: "the height of the section header should not be negative - if the height for section 0 is -0.00000"

Be that as it may, any value in 1 (except rounded 0 ) is considered negative - and using 1 as the return value, the header / footer space will reappear.

Is there a way around the solution?

Thanks in advance.

+5
source share
3 answers

I tried several values ​​for tableView(_:heightForHeaderInSection:) and found out that:

  • leastNormalMagnitude and leastNonzeroMagnitude will be considered as minus (hence the failure).
  • Zero will make TableView return the default height as the header / footer.
  • Anything between zero and one will be considered a minus.
  • As a result, TableView will return the default height.
  • Anything more than one (e.g. 1.1) will set the header / footer to the actual height.

I decided to use 1.1 to solve my problem.

Hope this helps someone out there!

+8
source

I have the same problem if I set the height of the section title with:

 tableView.sectionHeaderHeight = UITableViewAutomaticDimension tableView.estimatedSectionHeaderHeight = CGFloat.leastNormalMagnitude 

But if I set my controller as a delegate (UITableViewDelegate) for my kind of table and implement:

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

then it works

0
source

Perhaps CGFloat.leastNonzeroMagnitude is what you need!

0
source

Source: https://habr.com/ru/post/1264237/


All Articles