sectionHeaderTopPadding doesn't apply on Grouped and InsetGrouped UITableView
Asked Answered
V

2

1

since iOS15 a strange top gap started to appear

enter image description here

after research i found out that

if #available(iOS 15, *) {
    tableView.sectionHeaderTopPadding = 0
}

should solve the issue

however this works only for plain style table (UITableView.Style.plain), but my table is grouped and it looks like this property is taking no effect on GROUPED style (UITableView.Style.grouped)

is this a bug? how to remove the gap on grouped table?

Verbify answered 8/10, 2021 at 14:41 Comment(6)
Maybe that's just what grouped table views look like now. Look at the Home Screen table in the Settings app on your phone; it's the same way. If that's all you're talking about here, I would suggest that you just resolve to live with it.Spector
Also one might suggest that your screen shot shows an incorrect use of a grouped table style.Spector
not exactly, because the plain style sadly attaches the footer section header on bottom and the top section header on top while scrolling... so to avoid floating, sticky headers, you can resolve only to grouped table...Verbify
I do see your point. You could use a single column collection view in order to avoid the pinned headers.Spector
well yes, but this would however be a rework and possibly introduce other issues - and to do this just for a gap its overkill... i was looking more for a fixVerbify
If you think this is undesirable, and/or grouped style should respond to setting the top padding, file a bug report with Apple.Spector
B
3

According to the What's new in UIKit video from WWDC2021, top padding seems to have been added only to the plain style of UITableView.

Here's the part that mentions it

We have a new appearance for headers in iOS 15. For plain lists, section headers now display seamlessly in line with the content, and only display a visible background material when becoming pinned to the top as you scroll down. In addition, there's new padding inserted above each section header to visually separate the sections with this new design. You should use this plain style in conjunction with index bars for fast scrubbing when list content is long as demonstrated in the Contacts app.

https://developer.apple.com/videos/play/wwdc2021/10059/?time=438

Bandbox answered 16/12, 2021 at 7:35 Comment(1)
ok, but how to get rid of it?Verbify
A
1

You can try this (this worked for me for grouped/insetGrouped)

            if #available(iOS 15.0, *) {
                UITableView.appearance().sectionHeaderTopPadding = 0
                let tableHeaderView = UIView()
                tableHeaderView.translatesAutoresizingMaskIntoConstraints = false
                tableHeaderView.heightAnchor.constraint(equalToConstant: 5).isActive = true
                UITableView.appearance().tableHeaderView = tableHeaderView
            } else {
                // Fallback on earlier versions
            }
Anarchism answered 17/12, 2021 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.