Let's say we have the following code:
struct ContentView: View {
var body: some View {
VStack {
Color.black
.frame(width: 130, height: 30)
HStack(spacing: 17) {
ChildView()
ChildView()
ChildView()
ChildView()
}
}
}
}
struct ChildView: View {
var body: some View {
VStack(spacing: 2) {
Color.red
.frame(width: 15, height: 15)
Text("Title")
.font(.system(size: 10))
}
}
}
I want to stretch my HStack so that the first ChildView is aligned to the left edge of the red square and black rectangle, and the last one is aligned to the right edge of the red square and black rectangle. Like this:

I'm guessing it's possible to make a layout like this using custom alignmentGuide, but i'm stuck on it.

You can achieve this layout with the following changes to the code:
Apply the constraining width to the
VStackcontaining the black color and theHStackand applymaxWidth: .infinityto the black color.Set the spacing for the
HStackto 0.Add a
Spacerbetween each of the items in theHStack.Use a hidden placeholder to reserve vertical space for the labels.
Show the actual labels as an overlay, so that they do not impact the layout.
Like this: