繁体   English   中英

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

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

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

在此处输入图像描述

测试代码:

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才能没有间距。 默认情况下,它根据上下文采用默认空间

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)

暂无
暂无

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

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