There are three columns, if you look at the images the cells' y axis placement is row dependent. For example, regardless of the height of each image/cell the cells are spaced so that the row maintains alignment with its adjacent cells left and right. This creates awkward vertical white space. I want the cells to move up or down based on the vertical spacial allowance not creating white space in order to row-align with its neighbors. I like that LazyGrid loads the cells dynamically and reuses them.
Here is my code:
import SwiftUI
struct ContentView: View {
private var symbols = ["keyboard", "hifispeaker.fill", "printer.fill", "tv.fill", "desktopcomputer", "headphones", "tv.music.note", "mic", "plus.bubble", "video"]
private var colors: [Color] = [.yellow, .purple, .green]
var body: some View {
ScrollView {
LazyVGrid(columns: .init(3), alignment: .center, spacing: 10) {
ForEach((0...999), id: \.self) {
Image(systemName: symbols[$0 % symbols.count])
.font(.system(size: 30))
.frame(width: 50, height: CGFloat.random(in: 0...100))
.background(colors[$0 % colors.count])
.cornerRadius(10)
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
extension Array where Element == GridItem {
init(_ count: Int) {
self = Array(repeating: GridItem(.flexible()), count: count)
}
}