Extra padding above table view headers in iOS 15

IosUitableviewIos15

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

For applying changes everywhere in app

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

preferably in AppDelegate.

Solution 3 - Ios

Put this in the main didFinishLaunchingWithOptions to fix it globally:

if (@available(iOS 15.0, *))
{
	UITableView.appearance.sectionHeaderTopPadding = 0;
}

Solution 4 - Ios

A global way for obj-c:

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

Solution 5 - Ios

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
}

Solution 6 - Ios

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
}

Solution 7 - Ios

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.

Solution 8 - Ios

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.

Solution 9 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJakub TruhlářView Question on Stackoverflow
Solution 1 - IosJakub TruhlářView Answer on Stackoverflow
Solution 2 - IosHassyView Answer on Stackoverflow
Solution 3 - IosAaceView Answer on Stackoverflow
Solution 4 - IosMedhiView Answer on Stackoverflow
Solution 5 - IosAshuView Answer on Stackoverflow
Solution 6 - IosShani PatelView Answer on Stackoverflow
Solution 7 - IosAkifView Answer on Stackoverflow
Solution 8 - IosAndrew BailieView Answer on Stackoverflow
Solution 9 - IosFarhad BagherzadehView Answer on Stackoverflow