如何在 SwiftUI 中删除 List 和 ScrollView 的底部填充

How to remove bottom padding of List and ScrollView in SwiftUI

我想删除底部填充,红色 space 之间的白色 space。有什么办法可以实现吗?

测试代码:

struct ContentView: View {
    var body: some View {
        return NavigationView {
            VStack {
                // the same result with using List instead of ScrollView
                ScrollView {
                    ForEach(1..<100) { index in
                        HStack {
                            Spacer()
                            Text("\(index)")
                            Spacer()
                        }
                    }
                }.background(Color.red)
                HStack {
                    Spacer()
                    Text("Test")
                    Spacer()
                }
                .background(Color.red)
            }
            .navigationBarTitle(Text("Test"), displayMode: .inline)
        }
    }
}

你必须通过 0 才能没有空格。默认情况下,它根据上下文

采用默认值 space
VStack(spacing: 0) {

   // the same result with using List instead of ScrollView
   ScrollView {

   .........
}

只需使用 GeometryReader

@State var contentSize: CGSize = .zero

ScrollView {
    YourContentView()
        .overlay(
            GeometryReader { geo in
                Color.clear.onAppear {
                    contentSize = geo.size
                }
            }
        )
}
.frame(maxWidth: .infinity, maxHeight: contentSize.height)