ScrollViewReader anchor y offset

62 views Asked by At

I have a SwiftUI list view wrapped in ScrollViewReader. On change of date, I am using proxy.scrollTo(newDate, anchor: .top) to scroll to a section with corresponding date. I would like the scroll to align with the section header with some padding but whatever I try, I end up the view aligned with the first list row of the selected date (cutting off the section header).

Here is the full code:

ScrollViewReader { proxy in
            List(selection: $selectedFlights) {
                ForEach(flightStore.flightsGroupedByDate.prefix(sectionsLoaded), id: \.date) { groupedFlights in
                    Section(header: Text("\(groupedFlights.date.abbreviatedOmitted)")
                        .id(groupedFlights.date)
                        .foregroundStyle(Color.primary)
                        .fontWeight(.heavy)
                    ) {
                        ForEach(groupedFlights.flights, id: \.id) { flight in
                            ZStack {
                                NavigationLink(value: flight) { EmptyView() }.opacity(0)
                                FlightCell(flight: flight)
                                    .listRowInsets(EdgeInsets(top: 5, leading: 0, bottom: 5, trailing: 0))
                                    .swipeActions(edge: .leading) { swipeActionsLeading(for: flight) }
                                    .swipeActions(edge: .trailing) { swipeActionsTrailing(for: flight) }
                            }
                            .tag(flight.id)
                        }
                    }
                    .onAppear {
                        if groupedFlights == flightStore.flightsGroupedByDate[min(sectionsLoaded-1, flightStore.flightsGroupedByDate.count-1)] {
                            loadMoreSections()
                            print("loading more sections")
                        }
                    }
                }
            }
            .onChange(of: calendarVM.currentDate, perform: { newDate in

                withAnimation {
                    proxy.scrollTo(newDate, anchor: .top)
                }
            })
        }
0

There are 0 answers