SwiftUI 垂直列表部分 header 填充 iOS 15

SwiftUI Vertical List Section header padding in iOS 15

我有一个 ListSections :

    List {
        ForEach((1...3), id: \.self) { _ in
            Section(
                header: Text("My Section")
                            .font(.system(.title3))
                            .fontWeight(.bold)
                            .foregroundColor(.primary),
                content: {
                    ForEach((1...5), id: \.self) { _ in
                        Text("My Row")
                    }
                }
            )
        }
    }

这是 iPhone 8 模拟器上的结果 iOS 14.5:

这里是 iPhone 8 模拟器上 iOS 15 的结果:

我希望 iOS15 列表等于 iOS14.5 一个。我可以通过将 .listStyle(PlainListStyle()) 添加到 List 来删除水平填充,但是该部分的 header 仍然具有不同的垂直填充。

有没有办法使垂直 header 填充与 iOS14.5 相同?

环境:

使用 .listStyle() 修饰符将列表样式更改为 GroupedListStyle

.listStyle(GroupedListStyle())


Header 无灰色背景

.listStyle(PlainListStyle())

更多信息

https://developer.apple.com/documentation/swiftui/liststyle

您可以尝试在环境中设置defaultMinListHeaderHeight

List {
...
}
.environment(\.defaultMinListHeaderHeight, 16)

最后,最适合我的是添加

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

在 AppDelegate didFinishLaunchingWithOptions 函数中