What is the equivalent of distribution for GroupItem (If you want fillEqual, equalSpacing, equalCentering)
Scocher opened this issue · comments
Anton Axelsson commented
I would like to create two labels that have the same width and a spacing between them. Like fillEqual in stackview.
Is there a way to do this?
The best idea I have come up with is this:
vgroup.setItems {
vgroup.setItems {
HGroupItem(dataID: DataID.group, style: .init()) {
Label.groupItem(dataID: DataID.leading, content: .init(string: "Leading Label2"), style: .init())
.backgroundColor(.green)
VGroupItem(dataID: DataID.spacerGroup, style: .init(alignment: .centered(to: vgroup), spacing: 0)) {
SpacerItem(dataID: DataID.spacer, style: .init(fixedWidth: 20))
}
Label.groupItem(dataID: DataID.trailing, content: .init(string: "Trailing Label"), style: .init())
.backgroundColor(.red)
}
}
Tyler Hedrick commented
@Scocher In this example you need to have the labels have equal width constraints between each other, but the existing API of group items doesn't allow this type of relationship. I have a couple of recommendations:
- Create a simple component which conforms to
EpoxyableView
that wraps aUIStackView
with 2 labels set up the way you want. You could then use your new component directly in a group and get the benefits of the layout options thatUIStackView
provides. - Use
StaticGroupItem
to include an already initializedUIStackView
within your component or view controller. - You could try to get this to work using an
HGroup
with twoStaticGroupItems
and aSpacer
where you've set up the labels width relationship somewhere else. For example:
let label1 = Label(...)
let label2 = Label(...)
label1.widthAnchor.constraint(equalTo: label2.widthAnchor).isActive = true
hgroup.setItems {
StaticGroupItem(label1)
SpacerItem(fixedWidth: 20)
StaticGroupItem(label2)
}
I haven't run this code to see if it will work, but in theory this would get you what you want.