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

- 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 Answers
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
.

- 20,070
- 9
- 74
- 84
-
2
-
57why 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
-
1Has 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
-
2@SazzadHissainKhan Yes! I had a similar constraint and used `tableView.setValue(0, forKey: "sectionHeaderTopPadding")` – Zaheer Moola Apr 05 '22 at 15:34
-
-
For applying changes everywhere in app
if #available(iOS 15.0, *) {
UITableView.appearance().sectionHeaderTopPadding = 0.0
}
preferably in AppDelegate
.

- 18,301
- 9
- 84
- 152

- 5,068
- 5
- 38
- 63
Put this in the main didFinishLaunchingWithOptions to fix it globally:
if (@available(iOS 15.0, *))
{
UITableView.appearance.sectionHeaderTopPadding = 0;
}

- 1,792
- 16
- 15
-
7if #available(iOS 15.0, *) { UITableView.appearance().sectionHeaderTopPadding = 0 } – Am1rFT Sep 22 '21 at 10:50
-
-
2
-
@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
-
9If 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
A global way for obj-c:
if (@available(iOS 15.0, *)) {
[[UITableView appearance] setSectionHeaderTopPadding:0.0f];
}

- 2,656
- 23
- 16
-
UITableView.appearance.sectionHeaderTopPadding = 0; works fine in obj-c – trapper Jun 03 '22 at 07: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
}

- 3,373
- 38
- 34
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
}

- 2,592
- 12
- 30
- 40

- 51
- 1
- 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

- 20,070
- 9
- 74
- 84

- 136
- 3
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.

- 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
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.

- 27
- 6