1

I wanna create an HStack with levitating space between objects, but equal between each one and also it must fit on horizontal, Some of like that

enter image description here

enter image description here

I used Spacer() between objects, like that

HStack {
            Text("Object1")
            Spacer()
            Text("Object2")
            Spacer()
            Text("ASASASA")
            Spacer()
            Text("asa")
       }
       .padding()

But there is just a question, Is there any way better than for making my code cleaner??

mohsen
  • 4,698
  • 1
  • 33
  • 54

1 Answers1

1

Maybe something like this? You might want to set the alignment: to what you need.

HStack(spacing: 20) {
    Group {
        Text("Object1")
        Text("Object2")
        Text("ASASASA")
        Text("asa")
    }
    .frame(minWidth: 0, maxWidth: .infinity, alignment: .center)
}
Jan
  • 7,444
  • 9
  • 50
  • 74
  • I wanna have a levitating space between object, but There is a fix 20 pixel space between objects – mohsen Feb 25 '20 at 21:33
  • 1
    You might rephrase your original question. You first talk about a "equal space between object" then about a "levitating space between object" – Jan Feb 26 '20 at 08:42
  • 1
    try this article https://alejandromp.com/blog/2019/06/13/implementing-a-equally-spaced-stack-in-swiftui-thanks-to-tupleview/ – Jan Feb 26 '20 at 08:51