简体   繁体   中英

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

@State var contentSize: CGSize = .zero

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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