Skip to content
This repository has been archived by the owner on Nov 4, 2022. It is now read-only.

scrollPositionSetter is not working #220

Open
leogiroux opened this issue Sep 10, 2021 · 2 comments
Open

scrollPositionSetter is not working #220

leogiroux opened this issue Sep 10, 2021 · 2 comments

Comments

@leogiroux
Copy link

Hi,

Nothing is happening when I set the scroll position in my example, am I doing something wrong ?

    @State var asCollectionViewScrollPosition: ASCollectionViewScrollPosition? = ASCollectionViewScrollPosition.top

           ASCollectionView(sections: self.sections)
                .layout { sectionID in
                    if sectionID != "header" {
                        return self.getCategoryLayout()
                    } else if sectionID == "header" {
                        return self.getDefaultLayout(estimatedHeight: 130)
                    } else {
                        return self.getDefaultLayout()
                    }
                }
                .onScroll({ point, size in
                    print("point \(point.y)")
                    if point.y > 100  {
                        withAnimation(.spring()) { isTopBarVisible = true }
                    } else {
                        withAnimation(.spring()) { isTopBarVisible = false }
                    }
                })
                .scrollPositionSetter($asCollectionViewScrollPosition)
                .contentInsets(.init(top: 10, left: 0, bottom: 10, right: 0))
                .hideNavBar()

            Button {
                print("search button clicked")
                withAnimation {
                    asCollectionViewScrollPosition = ASCollectionViewScrollPosition.indexPath(IndexPath(row: 0, section: 0), positionOnScreen: .top, extraOffset: CGPoint(x: 0, y: 0))
                }
            } label: {
                Image(systemName: "magnifyingglass")
                    .resizable()
                    .foregroundColor(.white)
                    .frame(width: 24, height: 24, alignment: .topTrailing)
                    .padding()
            }
@kevindropwithus
Copy link

I'm experiencing the same

@r3pps
Copy link

r3pps commented Nov 10, 2021

I was experiencing this issue as well, but I have seemed to find a fix, what I did was added this to the updateUIViewController method in ASTableView file:

    context.coordinator.applyScrollPosition(animated: true)

This way, when the binding gets updated, the updateUIViewController method is triggered, and this forces the coordinator the apply the new scroll position. Hope this helps!

P.S. The same should also work by adding this line to the updateUIViewController method in the ASCollectionView file

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants