89

How to change the extra padding above UITableView section headers that has started to appear in iOS 15?

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
  • Damn, this almost made me cry I am not kidding. I have been trying to solve this for nearly 8-9 hours thinking I am doing something wrong with my table view section headers. – esh Nov 03 '22 at 21:47

9 Answers9

139

Since iOS 15, UITableView contains a new property called sectionHeaderTopPadding which specifies the amount of padding above each section header.

tableView.sectionHeaderTopPadding = 0.0

Note: This applies only to the UITableView.Style.plain.

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
97

For applying changes everywhere in app

if #available(iOS 15.0, *) {
    UITableView.appearance().sectionHeaderTopPadding = 0.0
}

preferably in AppDelegate.

Daniel Storm
  • 18,301
  • 9
  • 84
  • 152
Hassy
  • 5,068
  • 5
  • 38
  • 63
29

Put this in the main didFinishLaunchingWithOptions to fix it globally:

if (@available(iOS 15.0, *))
{
    UITableView.appearance.sectionHeaderTopPadding = 0;
}
Aace
  • 1,792
  • 16
  • 15
  • 7
    if #available(iOS 15.0, *) { UITableView.appearance().sectionHeaderTopPadding = 0 } – Am1rFT Sep 22 '21 at 10:50
  • @Aace suggested this global fix first here, hence +1; – Jeff Oct 05 '21 at 05:06
  • 2
    Are there any other surprises like this in iOS 15? – Jeff Oct 05 '21 at 05:41
  • @matt Point taken, but I don't see any sessions specifically about this issue or iOS 15, and there are too many for me to watch all of them. Can you recommend a specific session or two? – Jeff Oct 12 '21 at 06:55
  • 9
    If you introduce such a new property, why not default it to 0.0 to make it backwards compatible? You should not expect every developer to know every little change between OS versions, it's just crazy. – Werner Altewischer Oct 13 '21 at 08:22
11

A global way for obj-c:

if (@available(iOS 15.0, *)) {
    [[UITableView appearance] setSectionHeaderTopPadding:0.0f];
}
Medhi
  • 2,656
  • 23
  • 16
11

This is the new Instance Property of UITableView in iOS 15. https://developer.apple.com/documentation/uikit/uitableview/3750914-sectionheadertoppadding

sectionHeaderTopPadding which specifies the amount of padding above each section header.

To remove the padding use below code

if #available(iOS 15.0, *) {
    self.tableView.sectionHeaderTopPadding = 0.0 
}

To remove the padding from everywhere in the app, use below code in AppDelegate

if #available(iOS 15.0, *) {
    UITableView.appearance().sectionHeaderTopPadding = 0.0
}
Ashu
  • 3,373
  • 38
  • 34
5

Put this in the main didFinishLaunchingWithOptions to fix it globally

if (@available(iOS 15.0, *)) {
    [[UITableView appearance] setSectionHeaderTopPadding:0.0f];
}
if #available(iOS 15.0, *) {
    UITableView.appearance().sectionHeaderTopPadding = 0.0
}
Ruli
  • 2,592
  • 12
  • 30
  • 40
Shani Patel
  • 51
  • 1
  • 4
4

In case you are like me having trouble in some grouped style tableViews after setting sectionHeaderTopPadding but still seeing that annoying gap for the first section, just add the following in your VC's viewDidLoad:

tableView.tableHeaderView = UIView(frame: CGRect(x: .zero, y: .zero, width: .zero, height: CGFloat.leastNonzeroMagnitude))

Found in here

Jakub Truhlář
  • 20,070
  • 9
  • 74
  • 84
2

For Objective C version you can use the code below;

if (@available(iOS 15.0, *)) {
        [_tableViewC setSectionHeaderTopPadding:0.0];
        
};

where tableViewC is the target tableview.

Akif
  • 73
  • 5
  • Or to fix in all places, if (@available(iOS 15.0, *)) { [[UITableView appearance] setSectionHeaderTopPadding:0.0]; }; – Snips Nov 06 '21 at 11:54
1

For Xamarin Forms you can add the following code after the LoadApplication call in FinishedLaunching:

if(UIDevice.CurrentDevice.CheckSystemVersion(15, 0))
{
    void_objc_msgSend_nfloat(UITableView.Appearance.Handle, ObjCRuntime.Selector.GetHandle("setSectionHeaderTopPadding:"), 0);
}

I missed the version check and the app crashed on anything less than iOS15 without getting a crash report via TestFlight.