Skip to content

Instantly share code, notes, and snippets.

@qubblr
Last active February 17, 2022 12:22
Show Gist options
  • Save qubblr/f5d94dd0311d72dac5f1e75639b25f7b to your computer and use it in GitHub Desktop.
Save qubblr/f5d94dd0311d72dac5f1e75639b25f7b to your computer and use it in GitHub Desktop.
Method to get an index path for UICollectionView supplementary view
extension UICollectionView {
func indexPath(for supplementaryView: UICollectionReusableView?, ofKind kind: String = UICollectionView.elementKindSectionHeader) -> IndexPath? {
let elements = visibleSupplementaryViews(ofKind: kind)
let indexPaths = indexPathsForVisibleSupplementaryElements(ofKind: kind)
for (element, indexPath) in zip(elements, indexPaths) {
if element === supplementaryView {
return indexPath
}
}
return nil
}
}
@aplekhanov
Copy link

Awesome! Thanks for saving my day 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment