1

I've got a very basic view with a label in it in SwiftUI and I'm struggling to get it to align to the left. This is what it is:

VStack(alignment: .leading) {
  Label("Hi", ...)
}
.frame(maxWidth: .infinity)

No matter what I've tried the label stays in the center of the view. How can I fix this?

Nicolas Gimelli
  • 695
  • 7
  • 19
  • It is most likely one of the parent views that you haven't shown. Please post a [Minimal, Reproducible Example (MRE)](https://stackoverflow.com/help/minimal-reproducible-example). – Yrb Mar 09 '22 at 18:27

1 Answers1

8

The alignment parameter in the VStack initialiser determines that its child views will align to the stack's leading edge.

What you are missing is an alignment parameter in the frame modifier that tells the layout system how the content should position itself within your expanded frame.

VStack(alignment :.leading) { // alignment of the two labels
  Label("Hi", ...)
  Label("Second line", ...)
}
.frame(maxWidth: .infinity, alignment: .leading) // alignment of the whole stack in the larger frame
ScottM
  • 7,108
  • 1
  • 25
  • 42