有没有办法在
GridRow
内设置整个Grid
的背景?
下面的代码设置每个单独单元格的背景,但我想设置行的背景,而不管单元格之间的间距如何。请注意第 1 列和第 2 列之间的间隙。此功能对于显示在网格表中选择整行非常有用。
import SwiftUI
struct ContentView: View {
var body: some View {
Grid {
GridRow {
Text("Column 1")
Text("Column 2")
}
GridRow {
Text("Row 1 value 1")
Text("Row 1 value 2")
}
GridRow {
Text("Row 2 value 1")
Text("Row 2 value 2")
}
.background(Color.red)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
当我做标题行(跨 4 列)时,我使用了 ZStack。您也许可以根据您的用例重新调整它的用途:
GridRow {
ZStack {
Color.gray.gridCellUnsizedAxes([.horizontal, .vertical])
HStack {
Text("Full row").font(.headline).foregroundStyle(.white).padding()
Spacer()
Image(systemName: "arrow.up.forward.circle").padding(.trailing)
}
}
.cornerRadius(10)
.gridCellColumns(4)
}
.padding(.top)