Search code examples
swiftuicollectionviewuiimageviewuicollectionviewcell

Collection View cell off center when swiping


When I swipe through my collectionView cells they are cut off the page for some reason even tho the first cell is fine. When I get the the last cell then swipe again it bounces and then the last cell displays correctly. see video In the video the blue is an imageView in the cell. Green, orange and purple are collectionView cells and white is the collectionView background.


Solution

    • first give the value of collection cell spacing 0.
    • add the code for collection-layout.

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let height = UIScreen.main.bounds.height let width = UIScreen.main.bounds.width return CGSize(width: width, height: height) }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt Selection: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,minimumInteritemSpacingForSectionAt selection: Int) -> CGFloat{ return 0 }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,minimumLineSpacingForSectionAt selection: Int) -> CGFloat{ return 0 }

    if still it doesn't work then send your code... :)