[英]Alternate Background Color with LazyVGrid in SwiftUI
有没有办法更改 LazyVGrid 中交替行的背景颜色。 我没有看到这样做的方法。 我认为一种方法是使用 UITableView,但我想坚持使用 SwiftUI,而不使用 UIKit。
任何指示或方向都会非常有帮助。
谢谢
您可以尝试以下操作:
struct ContentView: View {
let columns: Int = 5
var body: some View {
let gridItems = Array(repeating: GridItem(.flexible(), spacing: 0), count: columns)
ScrollView(.vertical) {
LazyVGrid(columns: gridItems, spacing: 0) {
ForEach(0 ..< 20) { index in
Text("Item \(index)")
.padding(10)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(isEvenRow(index) ? Color.red: Color.green)
}
}
}
}
func isEvenRow(_ index: Int) -> Bool {
index / columns % 2 == 0
}
}
有用的链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.