I am having some trouble with ScrollView, I have the following code:
struct ContentView: View {
private enum Constants {
static let cellSize = CGSize(width: 100.0, height: 100.0)
static let rowSpacing: CGFloat = 8.0
}
@State private var rowCount = 1
@State private var contentOffset = CGPoint.zero
private var scrollViewHeight: CGFloat {
CGFloat(rowCount) * Constants.cellSize.height + (CGFloat(rowCount) - 1.0) * Constants.rowSpacing
}
var body: some View {
NavigationView {
ScrollView(.horizontal) {
ScrollView {
LazyVStack(spacing: Constants.rowSpacing) {
ForEach((0..<rowCount), id: \.self) { _ in
LazyHStack {
ForEach((1...20), id: \.self) {
Text("Cell \($0)")
.frame(
width: Constants.cellSize.width,
height: Constants.cellSize.height
)
.background(.red)
}
}
}
}
}
.scrollIndicators(.hidden)
}
.ignoresSafeArea(edges: .bottom)
.frame(maxHeight: scrollViewHeight)
.scrollIndicators(.hidden)
.toolbar {
Button("Add Row") {
rowCount += 1
}
Button("Remove Row") {
rowCount -= 1
}
}
.navigationTitle("Test")
}
}
}
For some reason the ScrollView content is centered vertically:
I need the content to align to the top. Any idea on how to fix this?


