airbnb / epoxy-ios

Epoxy is a suite of declarative UI APIs for building UIKit applications in Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What is the equivalent of distribution for GroupItem (If you want fillEqual, equalSpacing, equalCentering)

Scocher opened this issue · comments

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)
      }
    }

But then I get
Screenshot 2022-08-03 at 08 19 05
and this is what I want
Screenshot 2022-08-03 at 08 19 05

@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:

  1. Create a simple component which conforms to EpoxyableView that wraps a UIStackView 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 that UIStackView provides.
  2. Use StaticGroupItem to include an already initialized UIStackView within your component or view controller.
  3. You could try to get this to work using an HGroup with two StaticGroupItems and a Spacer 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.