1

I have a requirement in which the collection view should start at the centre of the screen and when I start scrolling, it should scroll to the left end. Also the last cell should scroll to the centre. If it is confusing please refer the images: My Current state:

My Current State

What I want at initial stage:

InitialStage

Also at the end of scrolling:

End of scrolling

I played with content offset a little but no success so far

1 Answers1

1

The content offset is the amount that the collection view (or actually the scroll view behind it) is currently scrolled.

What you are looking for is the contentInset.

You should be able to change this through the UICollectionViewLayout object that you are using. If you are using a UICollectionViewFlowLayout then it has a property for sectionInset.

If you do something like...

layout.sectionInset = UIEdgeInsets(top: 0, left: screenWidth * 0.5, bottom: 0, right: screenWidth * 0.5)

It should get you something like what you are looking for.

Fogmeister
  • 70,181
  • 37
  • 189
  • 274
  • 1
    Thank you. Works just as expected. –  Nov 02 '17 at 12:26
  • Can I add pagination like the cell should automatically fit at the centre point? Now it is like I can set the space between 4 & 5 at the centre, which is not good –  Nov 02 '17 at 12:35
  • @Arun Pagination only works where the page is the full width of the scroll view. But you could override the `contentOffsetForProposedContentOffset...` method. One sec, will get a link. – Fogmeister Nov 02 '17 at 12:38
  • 1
    @Arun https://stackoverflow.com/questions/13492037/targetcontentoffsetforproposedcontentoffsetwithscrollingvelocity-without-subcla/13493707#13493707 It's in Objective-C but will give you an idea of what you need to do. – Fogmeister Nov 02 '17 at 12:39