1

I'm trying to achieve the following grid layout with SwiftUI but not quite sure on the best approach.

enter image description here

My code is below and it's not quite getting what I want and also seems hacky to have many nested stacks

VStack {
            
            VStack {
                
                HStack {
                    
                    VStack {
                        
                        Text("Text Here")
                        Text("336.851")
                        
                    }
                    .padding(20)
                    .background(ColorManager.orange)
                    .cornerRadius(10)
                    
                    VStack {
                        
                        Text("Text Here")
                        Text("336.851")
                        
                    }
                    .padding(20)
                    .background(ColorManager.red)
                    .cornerRadius(10)
                    
                }
                
                HStack {
                    
                    VStack {
                        
                        Text("Text Here")
                        Text("336.851")
                        
                    }
                    .padding(20)
                    .background(ColorManager.green)
                    .cornerRadius(10)
                    
                    VStack {

                        Text("Text Here")
                        Text("336.851")

                    }
                    .padding(20)
                    .background(ColorManager.blue)
                    .cornerRadius(10)

                    VStack {

                        Text("Text Here")
                        Text("336.851")

                    }
                    .padding(20)
                    .background(ColorManager.purpleLight)
                    .cornerRadius(10)
                    
                }
                
                
            }
            
        }

My code gives the below result, I'm just not sure how I would max the boxes span half and a third of the screen. Also, is the approach I've taken with nested stack the right way?

enter image description here

CIB
  • 535
  • 1
  • 12
  • 35

2 Answers2

8

You may try this:

struct ContentView: View {
    var body: some View {
        VStack {
            HStack {
                cell(header: "Text Here", text: "336.851", color: Color.orange)
                cell(header: "Text Here", text: "336.851", color: Color.red)
            }
            HStack {
                cell(header: "Text Here", text: "336.851", color: Color.green)
                cell(header: "Text Here", text: "336.851", color: Color.blue)
                cell(header: "Text Here", text: "336.851", color: Color.purple)
            }
        }
    }

    func cell(header: String, text: String, color: Color) -> some View {
        HStack {
            VStack(alignment: .leading) {
                Text(header)
                    .font(.caption)
                Text(text)
                    .fontWeight(.semibold)
            }
            Spacer()
        }
        .frame(maxWidth: .infinity)
        .padding(20)
        .background(color)
        .cornerRadius(10)
        .padding(10)
    }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
0

Insert this code for each Box you have.

.padding(20)
.frame(maxWidth: .infinity) //This will stretch your Box.
.background(ColorManager.orange)
Alex B
  • 131
  • 7