'Extra padding above table view headers in iOS 15

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



Solution 1:[1]

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:[2]

For applying changes everywhere in app

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

preferably in AppDelegate.

Solution 3:[3]

Put this in the main didFinishLaunchingWithOptions to fix it globally:

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

Solution 4:[4]

A global way for obj-c:

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

Solution 5:[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
}

Solution 6:[6]

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:[7]

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:[8]

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:[9]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Daniel Storm
Solution 3
Solution 4 Medhi
Solution 5 Ashu
Solution 6 Ruli
Solution 7 Akif
Solution 8 Andrew Bailie
Solution 9 Jakub Truhlá?