Nicolas Gimelli
Nicolas Gimelli

Reputation: 1302

SwiftUI - Label not aligning to left

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?

Upvotes: 2

Views: 3147

Answers (1)

ScottM
ScottM

Reputation: 10414

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

Upvotes: 10

Related Questions