0

I'm trying to achieve a 2 in row tiles in my SwiftUI app but I cannot.

The tiles are 1 in a row and they are underneath each other.

This is my current code:

ScrollView(.vertical){
        
        VStack(){
                      
                    VStack(alignment: .trailing, spacing: 6) {
                          Image("flower")
                                   .resizable()
                                   .frame(width: 160, height: 200)
                                   .clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
                                      
                       
                        Text("2/2/2021")
                                   .background(Color.white.opacity(0.2))
                                   .font(.caption)
                
                        
                        Text("Some Info goes here because it can...")
                                  .font(.callout)
                                  .frame(width: 160)
                    
                
                                }.onTapGesture {
                                           // Handle the action when the card is touched
                                }

                    VStack(alignment: .trailing, spacing: 6) {
                          Image("flower")
                                   .resizable()
                                   .frame(width: 160, height: 200)
                                   .clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
                                      

                        Text("2/2/2021")
                                    .background(Color.white.opacity(0.2))
                                    .font(.caption)
                
                        
                        Text("Some Info goes here because it can...")
                                    .font(.callout)
                                    .frame(width: 160)
                    
                
                                }.onTapGesture {
                                           // Handle the action when the card is touched
                                 }
            
        }
 
        }

This is what I'm trying to achieve:

enter image description here

But this is what I have so far with my code:

enter image description here

Could someone please shed a light on this?

sheldor
  • 115
  • 12
drago
  • 1,148
  • 2
  • 16
  • 35

2 Answers2

2

It's pretty easy with LazyVGrid, specify the number of columns you want to display and add the views

struct TestView : View {
    
    var body : some View {
        let gridItems = [GridItem(.flexible()), GridItem(.flexible())]
        ScrollView {
            LazyVGrid(columns: gridItems, content: {
                ForEach(0..<20) { i in
                    ZStack {
                        RoundedRectangle(cornerRadius: 12)
                            .fill(Color.red)
                            .frame(width: 160, height: 200)
                        Text("\(i)")
                            .foregroundColor(.white)
                            .font(.largeTitle)
                    }
                }
            }).padding()
        }
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
1

I have created a Pure SwiftUI Collection View:

struct CollectionView<Content: View>: View {
    var arrayCount: Int
    let content: (_ index: Int) -> Content
    var body: some View {
        ScrollView {
            VStack(spacing: 20) {
                ForEach(0..<getDimesion().0) { index in
                    HStack {
                        Spacer()
                        ForEach(index * 2..<(index * 2) + 2) { index2 in
                            content(index2)
                            Spacer()
                        }
                    }
                }
                if getDimesion().1 {
                    HStack {
                        Spacer()
                        content(arrayCount - 1)
                        Spacer()
                        content(arrayCount - 1)
                            .hidden()
                        Spacer()
                    }
                }
                Spacer()
            }
        }
    }
    func getDimesion() -> (Int, Bool) {
        var toReturn = (0, false)
        if arrayCount.isMultiple(of: 2) {
            toReturn = (arrayCount / 2, false)
        }else {
            toReturn = ((arrayCount - 1) / 2, true)
        }
        return toReturn
    }
}

Usage:

CollectionView(arrayCount: array.count) { index in
    Image("Jupiter")
         .resizable()
         .frame(width: 160, height: 200)
         .clipShape(RoundedRectangle(cornerRadius: 12, style: .continuous))
}

Preview Unfortunately, it doesn't work with Identifiable yet!

Timmy
  • 4,098
  • 2
  • 14
  • 34