Using the following configuration:
let layout = UICollectionViewFlowLayout()
layout.sectionHeadersPinToVisibleBounds = true
let collectionViewController = UICollectionViewController(view.bounds, collectionViewLayout: layout)
The following code will scroll to the given index path but the item will be under and covered by its header:
let indexPath = IndexPath(section: 0, row: 2)
collecitonView.scrollToItem(at: indexPath, at: .top, animated: true)
How do I get the collection view to scroll to the item at indexPath without the item being covered by its section header when sectionHeadersPinToVisibleBounds is set to true?
It seems like a iOS framework's bug. I'm using this way.
guard let layout = collectionView.collectionViewLayout.layoutAttributesForItem(at: indexPath) else {
collectionView.scrollToItem(at: indexPath, at: .top, animated: true)
return
}
let offset = CGPoint(x: 0, y: layout.frame.minY - headerHeight)
collectionView.setContentOffset(offset, animated: true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With