Home > Back-end >  SwiftUI: how to add padding to the first item in VStack
SwiftUI: how to add padding to the first item in VStack

Time:02-21

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

CodePudding user response:

.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)
  • Related