I need to make a SwiftUI view that can be scrolled both vertically and horizontally, with multiple columns, where the leftmost ones don't scroll horizontally, but the following ones do.
Think like a spreadsheet with some frozen header columns.
I've got something working, kinda. The GIF shows when scrolling vertically it moves all of the cells, and scrolling horizontally only scrolls the "custom" cells.
However, I'd also like to have a frozen header row, like a Mac table view's column headings, that remains floating at the top, aligned with the fixed and horizontally scrolling columns of the rows below. I haven't figured that one out yet.
Here's my prototype code:
ScrollView {
ZStack(alignment: .leading) {
ScrollView (.horizontal) {
LazyVStack(spacing: 20) {
ForEach(0..<20) { row in
LazyHStack(spacing: 20) {
Text("")
.frame(width: 250, height: 200)
.background(Color.clear)
ForEach(0..<10) { column in
Text("Custom \(row), \(column)")
.foregroundColor(.white)
.font(.largeTitle)
.frame(width: 200, height: 200)
.background(Color.red)
}
}
}
}
}
VStack(spacing: 20) {
ForEach(0..<20) { row in
HStack(spacing: 2) {
Text("\(row)")
.foregroundColor(.yellow)
.font(.largeTitle)
.frame(width: 50, height: 200)
.background(Color.blue)
Text("Fixed \(row)")
.foregroundColor(.yellow)
.font(.largeTitle)
.frame(width: 200, height: 200)
.background(Color.blue)
}
}
}
}
}
I'm a newbie at SwiftUI, so perhaps I'm missing something obvious. Any advice much appreciated!