Make VStack fill images proportionally

48 Views Asked by At

I'm doing the switch from UIKit to SwiftUI and I'm trying to achieve the following "Collection View".

enter image description here

Assuming I have an array of pairs (i.e: [[<Image0>, <Image1>], <Image2>, <Image3>]]..), my plan was to:

  1. Create ScrollView
  2. Create HStack and embed it inside the ScrollView
  3. For each group, create VStack with some kind of filling proportional behavior

My current code:

struct AutoScrollingImages: View {
    let imageGroups: [[String]]
    
    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack {
                ForEach(0..<imageGroups.count, id: \.self) { groupIndex in
                        VStack(spacing: .zero) {
                            ForEach(imageGroups[groupIndex], id: \.self) { imageName in
                                Image(imageName)
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .cornerRadius(8.0)
                            }
                    }
                    .background(.gray)
                }
            }
        }.background(Color.red)
    }
}

But it fails horribly. I'd love help on this one.

0

There are 0 best solutions below