Before loading the collection view user sets the number of image in the array of collection view. All of the cells don't fit on the screen. I have 30 cells and only 6 on the screen.
The question: How to scroll to the cell with desired image automatically at the load of UICollectionView?
New, Edited Answer:
Add this in
viewDidLayoutSubviews
SWIFT
Normally,
.centerVertically
is the caseObjC
Old answer working for older iOS
Add this in
viewWillAppear
:I would recommend doing it in
collectionView: willDisplay:
Then you can be sure that the collection view delegate delivers something. Here my example:I totally agree with the above answer. The only thing is that for me the solution was set the code in viewDidAppear
When I used the viewWillAppear the scrolling didn't work.
this seemed to work for me, its similar to another answer but has some distinct differences:
Swift:
Swift 3
Scroll Position:
All answers here - hacks. I've found better way to scroll collection view somewhere after relaodData:
Subclass collection view layout what ever layout you use, override method prepareLayout, after super call set what ever offset you need.
ex: https://stackoverflow.com/a/34192787/1400119