删除 header 部分上方的额外填充?

Remove extra padding above section header?

我有一个固定高度的 List。我使用 listRowInsets.

删除了所有插图

但是,header 部分上方仍保留 22 点填充(滚动开始时缩小为零)。有没有办法 删除 header 部分上方的额外填充?

UPDATE: The extra padding only appears in iOS 15 runtimes.

如果我删除 header 部分(仅保留 ForEach 中的行),间距就会消失。

import SwiftUI
import Introspect

struct ContentView: View {
        
    var body: some View {
        ZStack {
            Grid().opacity(0.1)
            VStack {
                Spacer(minLength: 60)
                List {
                    Section(
                        header: HStack {
                            Text("Section Heaeder").font(.system(size: 40))
                            Spacer()
                        }
                            .frame(height: 60)
                            .background(Grid().opacity(0.4))
                            .listRowInsets(.zero),
                        content: {
                            ForEach(1...20, id: \.self) { eachRowIndex in
                                Text("Row \(eachRowIndex)")
                                    .frame(height: 40)
                                    .listRowInsets(.zero)
                                    .listRowBackground(
                                        Rectangle()
                                            .strokeBorder(Color.white.opacity(0.2), lineWidth: 1)
                                    )
                            }
                        }
                    )
                }
                .listStyle(.plain)
                .introspectTableView {
                    [=11=].separatorStyle = .none
                }
                .background(Grid().opacity(0.1))
                .padding(.leading, 20)
                .padding(.trailing, 25)
                .environment(\.defaultMinListRowHeight, 40)
            }
        }
        .edgesIgnoringSafeArea(.all)
    }
}

extension EdgeInsets {
    
    static var zero = EdgeInsets()
}

struct Grid: View {
    
    var body: some View {
        Rectangle()
            .fill(
                ImagePaint(image: Image("Square"))
            )
    }
}

使用sectionHeaderTopPadding它针对这个特定的间距。

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
        }
    }

    ...
}

对于 iOS 15 岁和旧

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