0

I want a grid of squares with no spaces between them. Currently, I use custom UICollectionViewLayout:

class BoardViewLayout: UICollectionViewLayout {
    override func collectionViewContentSize() -> CGSize {
        return self.collectionView!.frame.size
    }
    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        let boardSize = Int(sqrt(Double(self.collectionView!.numberOfItemsInSection(0))))
        let index = indexPath.row

        let size = self.collectionView!.frame.width / CGFloat(boardSize)
        let xIndex = CGFloat(index%boardSize)
        let yIndex = CGFloat(index/boardSize)

        let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        attributes.frame = CGRect(origin: CGPointMake(size*xIndex, size*yIndex), size: CGSizeMake(size, size))
        return attributes
    }
    override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        var attributesArray = [UICollectionViewLayoutAttributes]()
        for i in 0..<self.collectionView!.numberOfItemsInSection(0) {
            attributesArray.append(self.layoutAttributesForItemAtIndexPath(NSIndexPath(forItem: i, inSection: 0))!)
        }
        return attributesArray
    }
}

It looks like this: screenshot of the result

I need to get rid of the spaces between third and fourth row and third and fourth column (see the screenshot).
Later, there will be square images instead of only colours (borders won't change) and there will be background that has to perfectly fit the images (so it looks like one image if all squares are filled and borders disappear)

I know it strongly depends on how system scales images etc., but there must be way to do this.

Thank you for help

lukasbalaz7
  • 360
  • 2
  • 11
  • Already answered in http://stackoverflow.com/questions/17229350/cell-spacing-in-uicollectionview/23542017#23542017 – cynicaljoy Dec 26 '15 at 23:02

1 Answers1

0

You just need to set your minimum spacing to be 0:

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 0.0; }

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{ return 0.0; }

Joe Benton
  • 3,703
  • 1
  • 21
  • 17