1

I have a VStack with a list of items. I noticed that there's no padding for the first item. But there's padding for the gaps between each items. I am wondering how I can put a padding on the top.

enter image description here

The code looks like this:

VStack {
  item1
  item2
  item3
  Spacer()
}

I have tried to add a padding() on the top of VStack, but I got hang'ed without error message:

VStack {
  padding()
  item1
  item2
  item3
  Spacer()
}

2 Answers 2

2

.padding()adds padding to the view it is attached to (and it has to be attached to one, it can't stand alone).

what you want is a padding on the VStack:

VStack {
    item1
    item2
    item3
}
.padding()

or

.padding(.top)
Sign up to request clarification or add additional context in comments.

Comments

0

You shouldn't need any kind of padding if you used a NavigationView then a NavigationLink in a List. Example below.

link

struct BillsView: View {
    @State var name: String = ""
    @State var value: Double = 0.0
    var body: some View {
        List {
            TextField(text: $name, prompt: Text("Bill amount")) {
                Text("Bill Amount")
            }
            HStack {
                Text("Tip")
                Spacer()
                Slider(value: $value, in: 1...10)
            }
        }
        .navigationBarTitle("Bills", displayMode: .inline)
    }
    
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: BillsView()) {
                    Text("Navigation link here")
                }
            }
            .navigationBarTitle("Example", displayMode: .inline)
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.