Search code examples
iosswiftuitableviewios9ios10

Returning CGFloat.leastNormalMagnitude for UITableView section header causes crash


I made an app for iOS 8 which uses grouped UITableView for one of its page. There are multiple sections in it that uses CGFloat.leastNormalMagnitude (or CGFloat.min in Swift 2 and below) for section header and footer height to remove the "default" space. Everything went well until the app run in iOS 9 and 10, where it crashes with this error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'section header height must not be negative - provided height for section 0 is -0.00000'

Somehow, any value under 1 (except the rounded 0) is treated as a negative - and using 1 as return value will make the header / footer space appears again.

Is there any workaround to fix this?

Thanks in advance.


Solution

  • I have tried several values for the tableView(_:heightForHeaderInSection:), and found out that:

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

    I ended up using 1.1 for solving my problem.

    Hope this will help someone out there!