Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial index #79

Closed
indieSoftware opened this issue Sep 19, 2017 · 6 comments
Closed

Initial index #79

indieSoftware opened this issue Sep 19, 2017 · 6 comments

Comments

@indieSoftware
Copy link

There seems no possibility to set an initial index for the current item right in setting up the pager view. So it's not possible to start the pager view at a specific position rather than always with the first item.

The FSPagerView provides a function scrollToItem(at index: Int, animated: Bool) to scroll to a specific item. So after setting up the pager view and registering some cells I want to scroll without animation to a specific index (before the first frame has rendered), but I get an exception, that the index is out of bounds of the number of items the collection view has. That's because the numberOfItems is still 0 because the layout of the collection view hasn't yet evaluated its data source methods.

So the FSPagerView should either have a property like initialIndex or scrollToItem(at:animated:) should handle the scrolling to the index right after the collection view layout has been prepared if called before.

As a current workaround I can call scrollToItem(at:animated:) via GCD with zero delay in the next frame, so only one frame the wrong item will be shown.

@WenchaoD
Copy link
Owner

WenchaoD commented Sep 26, 2017

Do scrollToItem(at:animated:) in viewDidLayoutSubviews: and make sure it executes only one time. There is no initialIndex in UICollectionView, so it won't bother in this repo either.

@kornha
Copy link

kornha commented Jan 6, 2019

I'm not sure how this solution works. The viewDidLayoutSubviews gets called before the numberOfItemsAt gets called. I still get an error of an index OOB.

@kornha
Copy link

kornha commented Jan 6, 2019

Must be in main dispatch:

DispatchQueue.main.async {
self.pagerView.scrollToItem(at: index, animated: false)
}

@kornha
Copy link

kornha commented Jan 6, 2019

But with this solution I get one frame incorrectly shown.

@xinnai
Copy link

xinnai commented Jul 10, 2019

I got same problems ,I put scrollToItem in DispatchQueue and viewDidLayoutSubviews but still doesn't work at all

@troZee
Copy link

troZee commented Aug 23, 2021

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        pagerView.setNeedsLayout()
        pagerView.layoutIfNeeded()

        pagerView.scrollToItem(at: 1, animated: true)
     }

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

No branches or pull requests

4 participants