I am learning SwiftUI, but I have a problem... How can I scroll to top in ScrollView when the SegmentedControl value changed?
This is code for the main view to show Data:
var body: some View {
NavigationView {
SegmentedControl(selection: $selected) {
Text("Food").tag(0)
Text("Drinks").tag(1)
Text("Wines").tag(2)
}
.padding()
ScrollView {
VStack(alignment: .center) {
if selected == 0 {
ForEach (persons) {
AnyCell(person: $0, animal: nil, wine: nil)
}
} else if selected == 1 {
ForEach (animals) {
AnyCell(person: nil, animal: $0, wine: nil)
}
} else {
ForEach (wines) {
AnyCell(person: nil, animal: nil, wine: $0)
}
}
}
}
.navigationBarTitle(Text("Food list"), displayMode: .inline)
}
}
Thank you
