I'm late in the answering, but after spending almost a week to solve this issue I found a solution that solve my problem...
First, you need to add SwiftUIIntrospect to your project
List {
Text("Item before section #0")
ForEach(0..<10) { section in
Section {
ForEach(0..<6) { row in
Text("Item #\(section)-#\(row)")
}
} header: {
HStack {
Text("Section #\(section)")
.foregroundColor(.black)
Spacer()
}
.padding([.horizontal], 16)
.frame(height: 40.0)
.background(Color.cyan)
.listRowInsets(.zero)
}
}
}
.listStyle(.plain)
.introspect(.list, on: .iOS(.v15)) { tableView in
let identifier = "TableViewConfigured"
guard tableView.focusGroupIdentifier != identifier else {
return
}
tableView.focusGroupIdentifier = identifier
tableView.sectionHeaderTopPadding = 0
}
.introspect(.list(style: .plain), on: .iOS(.v16, .v17)) { collectionView in
let identifier = "CollectionViewConfigured"
guard
collectionView.focusGroupIdentifier != identifier,
let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewCompositionalLayout else {
return
}
collectionView.focusGroupIdentifier = identifier
let configuration = collectionViewLayout.configuration
configuration.interSectionSpacing = -22
collectionViewLayout.configuration = configuration
collectionView.setCollectionViewLayout(collectionViewLayout, animated: false)
}
PS: On iOS 16 or 17, if the first element of your list is a section, it will still have the issue. In my case, it's not a problem because I have a cell without a section before the first section...
Result: