SwiftUI 网格布局
SwiftUI Grid Layout
我正在尝试使用 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)
}
}
}
我的代码给出了以下结果,我只是不确定我如何才能最大程度地让方框占据屏幕的一半和三分之一。另外,我对嵌套堆栈采取的方法是否正确?
你可以试试这个:
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)
}
}
为您拥有的每个 Box 插入此代码。
.padding(20)
.frame(maxWidth: .infinity) //This will stretch your Box.
.background(ColorManager.orange)
我正在尝试使用 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)
}
}
}
我的代码给出了以下结果,我只是不确定我如何才能最大程度地让方框占据屏幕的一半和三分之一。另外,我对嵌套堆栈采取的方法是否正确?
你可以试试这个:
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)
}
}
为您拥有的每个 Box 插入此代码。
.padding(20)
.frame(maxWidth: .infinity) //This will stretch your Box.
.background(ColorManager.orange)