简体   繁体   English

SwiftUI中List和ScrollView的底部padding去除方法

[英]How to remove bottom padding of List and ScrollView in SwiftUI

I'd like to remove bottom padding, the white space between the red space.我想删除底部填充,红色空间之间的白色空间。 Is there any way to achieve it?有什么办法可以实现吗?

在此处输入图像描述

Test Code:测试代码:

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

You have to pass 0 for no spacing.您必须传递0才能没有间距。 By default it takes default space based on context默认情况下,它根据上下文采用默认空间

VStack(spacing: 0) {

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

   .........
}

Just use GeometryReader只需使用 GeometryReader

@State var contentSize: CGSize = .zero

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM