I am trying to align a dynamic collection (ie of varying count) of views to the right of a centered view, without moving the center view from its original, center position.
For example, the center view is Text("12")
and the views to the right are [Text("+3"), Text("+10"), Text("-1")]
so I want the views to the right of 12
be displayed to its right, while 12
is centered horizontally on the screen:
| 12 +3 +10 -1 |
If I try using an HStack
the 12
will move away from center. Is there a view modifier I can use? ex. Text("12").alignToRight(...)
This solution aligns the collection to the left of the right edge of the screen, but that's not the same thing.
ZStack {
Text("12")
HStack(spacing: 4) {
Spacer()
Text("+3")
Text("+10")
Text("-1")
}
}
I would prefer not to have to duplicate the HStack
on the left side and then make it disappear so as to balance the HStack
on the right (that is a ridiculous way of creating a layout, plus the example I'm providing here is simple, in actuality I have to use a ForEach
over the dynamic collection), ie
HStack {
Spacer()
HStack(spacing: 4) {
Spacer()
Text("+3")
Text("+10")
Text("-1")
}
.opacity(0)
Text("12")
HStack(spacing: 4) {
Spacer()
Text("+3")
Text("+10")
Text("-1")
}
Spacer()
}
Using something like Text("12").overlay(myCollectionView.offset(x: 16)
is also winging it, as the font size of the center text will vary, and so I'd have to guess and adjust the offset manually as well -- I'd rather have a padding between the two views.