4

Hello SwiftUI community,

I'm trying to do something that's probably easy to do but I'm stuck on it since hours. In a list, I'd like to have items composed of:

  • A text centered horizontaly (a number)
  • A text on the left of the number
  • A text on the right of the number

enter image description here

I've tried many things (alignments, aligmentguides, GeometryReader....) but didn't find the way to achieve the result above. Does anyone has an example to help me?

Thanks

Jul
  • 1,039
  • 3
  • 12
  • 20

1 Answers1

9

Here is possible solution. Tested with Xcode 11.4 / iOS 13.4

demo

A view for List row

struct DemoCenteredNumberView: View {
    var value: Int
    var body: some View {
        HStack {
            Spacer().overlay(
                Text("Text on left side")
                    .frame(maxWidth: .infinity, alignment: .trailing)
            )
            Text("\(value)").padding()
            Spacer().overlay(
                Text("Text on right")
                    .frame(maxWidth: .infinity, alignment: .leading)
            )
        }
    }
}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690