GarySabo
GarySabo

Reputation: 6710

SwiftUI: Scrolling Sticking when using LazyVGrid inside a Scrollview:

The below code seems to work fine on an iPhone 13 mini but stutters or sticks when scrolling on an iPhone 13, not every time but often. The code for the Calendar is here. What could be causing this? 🤔

struct MasterCalendarWithDay: View {
    
    @Environment(\.calendar) var calendar
    
    @State private var selectedDate: Date?
    
     @State private var selectedTabIndex = 0
    
    private var month: DateInterval {
        //calendar.dateInterval(of: .year, for: Date())!
        DateInterval(start: Calendar.current.date(byAdding: .month, value: show6Months ? -6 : -1, to: Date())!, end: Date())
    }
    
    var body: some View {
        LoadingView(isShowing: $isLoadingRecoveryData, text: show6Months ? "Loading past 6 months of data.  This may take a few seconds." : "Loading...") {
            NavigationView {
                ScrollView {
                    ScrollViewReader { value in
                        VStack {
                            ZStack {
                                CalendarView(interval: month) { date in
                                    if date <= Date() {
                                    Button(action: { self.selectedDate = date }) {
                                      //Omitted
                                      
                                    }.buttonStyle(PlainButtonStyle())
                                    }
                                } // end of calender
                                .onAppear {
                                    value.scrollTo(Date().startOfDay())
                            }
                        } //end of Zstack
                    }
                }
            } //end of scroll view
            .coordinateSpace(name: "pullToRefreshInRecoveryCalendar")
            .navigate(using: $selectedDate, destination: makeDestination) 
            }
            .navigationViewStyle(.stack) //This asks SwiftUI to only show one view at a time, regardless of what device or orientation is being used, and prevents constraint warnings in console log.  From: https://www.hackingwithswift.com/articles/216/complete-guide-to-navigationview-in-swiftui
             .onChange(of: selectedTabIndex, perform: { value in
        })
        }
    }

Upvotes: 1

Views: 1458

Answers (1)

GVG
GVG

Reputation: 609

I think your ScrollView should be nested inside the ScrollViewReader.

var body: some View {
    ScrollViewReader { value in
        ScrollView {
                ...

See https://developer.apple.com/documentation/swiftui/scrollviewreader

Upvotes: 0

Related Questions