67

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

Jakub Truhlář
  • 18,570
  • 9
  • 72
  • 80

9 Answers9

100

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ář
  • 18,570
  • 9
  • 72
  • 80
  • 1
    Is there anyway to do this per section? – Berry Blue Sep 25 '21 at 21:24
  • 33
    why in the world should this not be set to 0 as a default? I have scratched my head for one hour while migrating my app to SDK 15 – Fabio Napodano Oct 05 '21 at 11:08
  • @FabioNapodano Apparantly it's because of the new default look of the headers in iOS 15. The default offset of the text etc. is different as well. – Leon Lucardie Oct 19 '21 at 15:37
  • 1
    Has anyone experienced an issue where this code will not compile on Xcode 12, even if you wrap it in `if #available(iOS 15.0, *)`? – dcaraujo Oct 27 '21 at 09:54
  • 1
    @dcaraujo You will need to use Xcode `13.x` in order to be able to consume the `tableView.sectionHeaderTopPadding` property. – Vijay Tholpadi Nov 08 '21 at 05:54
  • 1
    @VijayTholpadi if he wants not to consume that property for lower version of os i.e. ios14.x, is there any way around to build it in xcode 12.x? – Sazzad Hissain Khan Jan 13 '22 at 05:06
  • 1
    @SazzadHissainKhan Yes! I had a similar constraint and used `tableView.setValue(0, forKey: "sectionHeaderTopPadding")` – Zaheer Moola Apr 05 '22 at 15:34
78

For applying changes everywhere in app

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

preferably in AppDelegate.

Daniel Storm
  • 17,279
  • 7
  • 80
  • 145
Hassy
  • 4,642
  • 3
  • 34
  • 60
25

Put this in the main didFinishLaunchingWithOptions to fix it globally:

if (@available(iOS 15.0, *))
{
    UITableView.appearance.sectionHeaderTopPadding = 0;
}
Aace
  • 1,682
  • 15
  • 14
  • 5
    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
  • 1
    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
  • 5
    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
7

A global way for obj-c:

if (@available(iOS 15.0, *)) {
    [[UITableView appearance] setSectionHeaderTopPadding:0.0f];
}
Medhi
  • 1,481
  • 13
  • 11
5

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,125
  • 35
  • 30
3

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,403
  • 12
  • 27
  • 35
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
  • 53
  • 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.

0

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ář
  • 18,570
  • 9
  • 72
  • 80