【问题标题】:How to remove bottom padding of List and ScrollView in SwiftUI如何在 SwiftUI 中删除 List 和 ScrollView 的底部填充
【发布时间】:2020-09-23 07:10:55
【问题描述】:

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

测试代码:

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

【问题讨论】:

    标签: swift swiftui swiftui-list


    【解决方案1】:

    你必须通过0 没有空格。默认情况下,它会根据上下文占用默认空间

    VStack(spacing: 0) {
    
       // the same result with using List instead of ScrollView
       ScrollView {
    
       .........
    }
    

    【讨论】:

    • 我试过了,不是这样吗? List 视图的底部和下一个视图之间仍然存在间隙,无论它是否嵌入到间距为 0 的 VStack 中。看到这里,因为我正在尝试解决类似的问题。 stackoverflow.com/questions/67579763/…
    【解决方案2】:

    只需使用 GeometryReader

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-01
      • 2019-12-29
      • 2018-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      相关资源
      最近更新 更多