Raphael Royer-Rivard
Raphael Royer-Rivard

Reputation: 2252

Force UICollectionView to scroll until a specific cell is centered

First of all, I already read How to force UICollectionView with fewer items to scroll? and it did not solve my problem.

I have an horizontal UICollectionView with 5 cells, and only 5 cells are needed to fill the screen width. This means the 3rd cell is centered. How can I scroll so the 4th cell is centered?

When using scrollToItemAtIndexPath, it won't scroll since there is not enough cells to scroll. In other words, if I want my 4th cell to be centered, there would be a blank space at the end since I only have 5 cells (and vice-versa for the 2nd cell).

Is adding empty cells the only solution?

Upvotes: 1

Views: 432

Answers (1)

skwashua
skwashua

Reputation: 1627

You could adjust the insets on the collection view. Add enough space that you can center the last cell.

More info from Apple

Upvotes: 2

Related Questions