SwiftUI - place two list side-by-side

Viewed 534

I am trying to place two list side by side. I want the lists start from the same point and that the longer list can go on. This is my code:

            HStack {
            HStack(spacing: 24) {
                VStack {
                    ForEach(0..<6) { integer in
                        Text("\(integer)")
                    }
                }
            }
            HStack(spacing: 24) {

                VStack {
                    ForEach(0..<4) { integersDEX in
                        Text("\(integersDEX)")
                    }
                }
            }
        }

This is the result:

0

1 0

2 1

3 2

4

This is what I would like:

0 0

1 1

2 2

3

5

What I'm doing wrong?

1 Answers

I assume you want to align both at top, so use

HStack(alignment: .top) {      // << here !!
    HStack(spacing: 24) {
        VStack {
            ForEach(0..<6) { integer in
                Text("\(integer)")
            }
        }
    }
    HStack(spacing: 24) {

        VStack {
            ForEach(0..<4) { integersDEX in
                Text("\(integersDEX)")
            }
        }
    }
}

enter image description here

Prepared with Xcode 12.4

Related