ScrollToItem is not working

try this code...

[collectionView setDelegate:self];
[collectionView reloadData];
[collectionView layoutIfNeeded];
[collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];

Swift - 4.x

collectionView.delegate = self
collectionView.reloadData()
collectionView.layoutIfNeeded()
collectionView.scrollToItem(at: IndexPath(item: 0, section: 0), at: .top, animated: true)

When the scroll direction is horizontal you need to use left,right or centeredHorizontally.

top is for the vertical direction.

collectionView.scrollToItem(at: index, at: .top, animated: true)

the best way i found to do this is to not use scrollToItem but to get the CGRect of the index and then make that visible.

 let rect = self.collectionView.layoutAttributesForItem(at: IndexPath(row: 5, section: 0))?.frame
 self.collectionView.scrollRectToVisible(rect!, animated: false)

My problem is a little weird. My iPhone 6/7/8 works fine, but iPhone X or 11 cannot scroll to my expected position. My original code is :

collectionView.reloadData()
collectionView.scrollToItem(at: IndexPath(item: 10, section: 0), at: .top, animated: true)

After I add collectionView.layoutIfNeeded(). Problem solves on my iPhone X and 11.

collectionView.reloadData()
collectionView.layoutIfNeeded()
collectionView.scrollToItem(at: IndexPath(item: 10, section: 0), at: .top, animated: true)

This answer is inspired by @ram. He should get the credit. Add this answer is just to emphasize the difference between different iPhones. So people can search their answer quicker.