Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 15: Remove empty space before cells in UITableView

I am using UITableView for a multi-section list. The issue I am seeing is a space above the cells of each section, even if I set tableView(_:heightForHeaderInSection:) to be 0. This occurs even when there is only one section and I set tableView(_:viewForHeaderInSection:) to be nil.

I have tried all other answers on StackOverflow relating to inset overrides/edge expanding but none have worked.

Example:

Screenshot of iOS table view. Above each section header is a gap which is the same colour as the table background.

like image 693
Amja Avatar asked Sep 25 '21 00:09

Amja


1 Answers

Check if you are only seeing this issue on iOS 15. If so, this may be caused by the newly introduced UITableView.sectionHeaderTopPadding property. You will need to set this value to 0 in order to remove the spacing before section headings:

let tableView = UITableView()
tableView.sectionHeaderTopPadding = 0

// Etc.

This property is only available in iOS 15 so you will need an API check if building for earlier versions.

If you're not on iOS 15, this question has most of the answers to this issue.

like image 94
Amja Avatar answered Oct 17 '22 20:10

Amja