Extra Padding Above Table View Headers in iOS 15

Extra padding above table view headers in iOS 15

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.

iOS 15 - UItableView top padding issue

After a lot of research, I founded the answer in Apple developer documentation:
https://developer.apple.com/documentation/uikit/uitableview/3750914-sectionheadertoppadding?language=objc

So, to solve this problem I added this code in all screen that I was using UITableView:

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

With this code the gap goes away.

How do I get rid of this extra padding above each section in my table view that appeared in iOS 15?

Ok, I figured it out. It's a one-line fix:

self.tableView.sectionHeaderTopPadding = 0.0f;

Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7

I have found the cause of my original bug and created a sample project showcasing it. I believe there is an iOS7 bug.

As of iOS7, if you create a UITableView with the Grouped style, but do not have a delegate set on first layout, then you set a delegate and call reloadData, there will be a 35px space at the top that will never go away.

See this project I made showcasing the bug: https://github.com/esilverberg/TableViewDelayedDelegateBug

Specifically this file: https://github.com/esilverberg/TableViewDelayedDelegateBug/blob/master/TableViewDelayedDelegateBug/ViewController.m

If line 24 is active,

[self performSelector:@selector(updateDelegate) withObject:nil afterDelay:0.0];

there will be an extra 35 px space at the top. If line 27 is active and 24 is commented out,

self.tableView.delegate = self;

no space at the top. It's like the tableView is caching a result somewhere and not redrawing itself after the delegate is set and reloadData is called.

iOS 15: Remove empty space before cells in UITableView

Check if you are only seeing this issue on iOS 15. If so, this may be caused by the newly introduced UITableView.sectionHeaderTopPadding property. You will need to set this value to 0 in order to remove the spacing before section headings:

let tableView = UITableView()
tableView.sectionHeaderTopPadding = 0

// Etc.

This property is only available in iOS 15 so you will need an API check if building for earlier versions.

If you're not on iOS 15, this question has most of the answers to this issue.

Remove extra padding above section header?

Use sectionHeaderTopPadding. It targets this specific spacing.

The API is only available from iOS 15, but the extra spacing itself is present in iOS 15 runtimes only.

struct ContentView: View {

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

...
}

Sample Image

How to remove section header separator in iOS 15

Option 1:
Maybe by using UITableViewCellSeparatorStyleNone with the table view and replacing the system background view of the cell with a custom view which only features a bottom line?

Option 2: Using hint from https://developer.apple.com/forums/thread/684706

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 150000 // only Xcode 13+ needs and can compile this
if (@available(iOS 15.0, *)) {
[self.tableview setSectionHeaderTopPadding:0.0f];
}
#endif
}

Extra Space Between UITableViewSections in iOS 15

In iOS 15 the property sectionHeaderTopPadding was added. It affects that exact space. The property's default value is automaticDimension. Setting it to 0.0 fixes the problem.

Since the property is only available in iOS 15, you may want to wrap it with an availability block:

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

Here's the original code snippet from the question including necessary changes:

import UIKit

final class ViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()

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

// The rest is without changes.

}

Here's the output in iOS 15 after the change:

iOS 15 after update



Related Topics



Leave a reply



Submit