mercredi 22 juin 2016

Equal spacing around UICollectionViewCells

I want to have equal amount of spacing between all the cells as well as between the screen border and the closest cell. Basically something like this. Equal spacing in and all around.

enter image description here

I managed to test this in the interface builder by setting values like this. I set the cell size to 110, Min spacing to 0 and all Section Insets to 10.

enter image description here

And it looks perfect in the IB.

enter image description here

However when I run it (in an iPad Air 2), it looks like this.

enter image description here

Notice the spacing in between the cells is bigger. I also tried to do this solely from the code by implementing the following methods from the UICollectionViewDelegateFlowLayout.

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSize(width: 110, height: 110)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 0
}

That yielded the same results. Can anybody please tell me how to correct this? I'd prefer to do this by code. But I'm open to anything.

Aucun commentaire:

Enregistrer un commentaire