SwiftUI网格布局

我正在尝试使用SwiftUI实现以下网格布局,但不确定最佳方法。

SwiftUI网格布局

我的代码在下面,并没有得到我想要的东西,而且似乎很笨拙,有很多嵌套堆栈

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)
                    
                }
                
                
            }
            
        }

我的代码给出了以下结果,但我不确定如何将框最大化到屏幕的一半和三分之一。另外,我对嵌套堆栈采用的方法是否正确?

SwiftUI网格布局

iCMS 回答:SwiftUI网格布局

您可以尝试以下方法:

struct ContentView: View {
    var body: some View {
        VStack {
            HStack {
                cell(header: "Text Here",text: "336.851",color: Color.orange)
                cell(header: "Text Here",color: Color.red)
            }
            HStack {
                cell(header: "Text Here",color: Color.green)
                cell(header: "Text Here",color: Color.blue)
                cell(header: "Text Here",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)
    }
}
,

为您拥有的每个Box插入此代码。

.padding(20)
.frame(maxWidth: .infinity) //This will stretch your Box.
.background(ColorManager.orange)
本文链接:https://www.f2er.com/2058453.html

大家都在问