I am trying to put a scrollable horizontal list at the top of the screen stacked on top of the map. I can put a Text view there, but when I try to put a ScrollView there, it stays in the center. What am I doing wrong?
My Code
//
// TestUIFile.swift
//
import SwiftUI
import MapKit
struct TestUIFile: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))
var body: some View {
ZStack(alignment: .top) {
Map(coordinateRegion: $region)
Text("Why am I at the top?")
//but I am in the center?
ScrollView(.horizontal) {
LazyHStack {
ForEach(0...50, id: .self) { index in
Text(String(index))
}
}
}
}
}
}
struct TestUIFile_Previews: PreviewProvider {
static var previews: some View {
TestUIFile()
}
}
2
Answers
I figured it out.
This is a vertical layout, so you should be using
VStack
, notZStack
.Result: