import SwiftUI
struct LazyStacksDemoView: View {
var body: some View {
VStack {
Text("Lazy Stacks Demo")
.font(.title)
.padding()
ScrollView {
LazyVStack(spacing: 10) {
ForEach(1...50, id: \.self) { i in
Text("Item \(i)")
.multilineTextAlignment(.center)
.frame(maxWidth: .infinity)
.padding()
.background(Color.gray.opacity(0.2))
.cornerRadius(8)
}
}
.padding()
}
ScrollView(.horizontal) {
LazyHStack(spacing: 10) {
ForEach(1...20, id: \.self) { i in
Text("Item \(i)")
.multilineTextAlignment(.center)
.frame(width: 80, height: 50)
.padding()
.background(Color.gray.opacity(0.2))
.cornerRadius(8)
}
}
.padding()
}
}
.padding()
}
}
struct LazyStacksDemoView_Previews: PreviewProvider {
static var previews: some View {
LazyStacksDemoView()
}
}We use LazyVStack inside a vertical ScrollView to efficiently show 50 items vertically. Each item is a Text with padding and a light gray rounded rectangle background for a clean look.
Similarly, LazyHStack inside a horizontal ScrollView shows 20 items horizontally. Each item has fixed width and height to keep consistent size and uses the same styling.
Padding and spacing between items improve readability and touch targets. The title is styled with a larger font and padding at the top.