swiftuiscrollviewswiftui

How to set contentInset of a ScrollView in SwiftUI


I can't seem to find how to set the contentInset of a ScrollView. My goal is to make the last object in my ScrollView above the Purple Main Button.

enter image description here

If there is a command, could someone help how to implement this into my current code below. I would appreciate your help!

struct Overview: View {
    var body: some View {
        
        NavigationView {
            
            ScrollView(showsIndicators: false) {
                VStack(spacing: 10) {
                    ForEach(0..<5) {
                        Text("Item \($0)")
                            .foregroundColor(.white)
                            .font(.largeTitle)
                            .frame(width: 340, height: 200)
                            .background(Color("Boxes"))
                            .cornerRadius(10)
                    }
                }
                .frame(maxWidth: .infinity)
            }
            .navigationBarHidden(false)
            .navigationBarTitle("Overview", displayMode: .automatic)
        }
    }
}

Solution

  • You could put an invisible view underneath your ScrollView content and give it bottom padding.

    For example with Color.clear and a bottom-padding of 300.

    struct Overview: View {
        var body: some View {
            
            NavigationView {
                
                ScrollView(showsIndicators: false) {
                    VStack(spacing: 10) {
                        ForEach(0..<5) {
                            Text("Item \($0)")
                                .foregroundColor(.white)
                                .font(.largeTitle)
                                .frame(width: 340, height: 200)
                                .background(Color("Boxes"))
                                .cornerRadius(10)
                        }
    
                        Color.clear.padding(.bottom, 300)
                    }
                    .frame(maxWidth: .infinity)
                }
                .navigationBarHidden(false)
                .navigationBarTitle("Overview", displayMode: .automatic)
            }
        }
    }