I'm looking for a way to remove top section padding in my SwiftUI List
With iOS 15, we can do UITableView.appearance().sectionHeaderTopPadding = 0
However, with iOS 16, List has been reimplemented with UICollectionView and I couldn't find a way to remove the section header top padding
Here is sample code
import SwiftUI
struct TaskRow: View {
var body: some View {
Text("Task data goes here")
}
}
struct HeaderText: View {
var text:String
var body: some View {
Text(text)
.font(.system(.title3))
.fontWeight(.bold)
.foregroundColor(.primary)
}
}
struct ListWithSections: View {
init() {
if #available(iOS 15.0, *) {
UITableView.appearance().sectionHeaderTopPadding = 0
} else {
// Fallback on earlier versions
}
}
var body: some View {
if #available(iOS 16.0, *) {
List {
Section(header: HeaderText(text: "Section 1")) {
TaskRow()
TaskRow()
TaskRow()
}
Section(header: HeaderText(text: "Section 2")) {
TaskRow()
TaskRow()
TaskRow()
}
}
.scrollContentBackground(.hidden)
.background(Color.gray)
.listStyle(.plain)
} else {
List {
Section(header: HeaderText(text: "Section 1")) {
TaskRow()
TaskRow()
TaskRow()
}
Section(header: HeaderText(text: "Section 2")) {
TaskRow()
TaskRow()
TaskRow()
}
}
.background(Color.gray)
.listStyle(.plain)
}
}
}
Images for comparing between iOS 16 and iOS 15
Any way we can achieve it with List Plain Style?
Update: Using ListRowInset as @Yrb suggested will give almost same UI. However when we scroll, we have minor difference
Here what scroll in ios15 looks like (what I want to achieve in ios16)
Here what scroll in ios16 looks like (with listRowInset as @Yrb suggested)
Thanks in advance.
Best regards.