I have custom layout with fullscreen cells. When removing cell from the left (it's not visible at the time), UICollectionView jumps to the next cell. It's like current cell was at index 4 and when cell on the left removed the next cell has index 4 now and immediately scroll to the next cell. Describing in 3 steps (A is cell that need to be fullscreen, x will be removed, o other cells, large letter is fullscreen):
- ooooAoo
- oooxAoo
- oooaOo
But must keep this oooAoo
Here is my solution if it can help to anybody, did not found how to achieve desired offset natively:
//some model manipulating code, removing desired items here...
let currentList = currentCell?.parentList
reloadData()
if let list = currentList, let index = self.lists.firstIndex(of: list) {
self.scrollToItem(at: IndexPath(row: index, section: 0), at: .centeredHorizontally, animated: false)
}
- currentCell is computed property, returns optional middle cell. Detect which cell is the largest, because of custom flowlayout logic.
- parentList is the model item, I can compare cell by it to make life easier. I check which list was attached to the cell before reloadData().