How do I set collection view's cell size via the auto layout

前端 未结 5 614
逝去的感伤
逝去的感伤 2020-12-05 07:45

Hi I\'m trying to resize the cell via the auto layout.

I want display the cell by the 3 by 3.

First Cell\'s margin left=0

Last Cell\'s m

5条回答
  •  执念已碎
    2020-12-05 08:17

    Here is another solution but It only works on iPhone6.

    I will trying to update for iphone5/6plus

    Special thanks to @vacawama!

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    
    
            // Compute the dimension of a cell for an NxN layout with space S between
            // cells.  Take the collection view's width, subtract (N-1)*S points for
            // the spaces between the cells, and then divide by N to find the final
            // dimension for the cell's width and height.
    
            let cellsAcross: CGFloat = 3
            let spaceBetweenCells: CGFloat = 1
            let dim = (collectionView.bounds.width - (cellsAcross - 1) * spaceBetweenCells) / cellsAcross
                    print(indexPath.row)
    
            var width = dim
    
            //last cell's width
            if(  (indexPath.row + 1) % 3 == 0){
                width = 124
    
            }else {
                width = 124.5
            }
            print(width)
            return CGSizeMake(width, dim)
    
        }
    
        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
            return 1;
        }
    
        func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
            return 1;
        }
    

提交回复
热议问题