How to center a UICollectionView when it is tapped?

前端 未结 4 844
说谎
说谎 2021-02-01 08:25

I have a UICollectionView and it has quite a lot of UICollectionViewCells on it. I want to scroll the cell to the centre of the UICollectionView<

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-01 08:54

    Swift 3 solution for scrolling and centering screen to make the tapped item visible:

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
      collectionView.scrollToItem(at: indexPath, at: .centeredVertically, animated: true)
    }
    

    This doesn't add inset above or under the collectionView!

提交回复
热议问题