Hugo
Hugo

Reputation: 767

Swift: border on UICollectionViewCell for some sides

For an app I am currently building I need a grid like UICollectionView with 3x3 UICollectionViewCell's displayed. The UICollectionView will have the same width as the screen, and therefore I want a border only shown on the right and bottom side of the UICollectionViewCell. It would have to look like this:

    -----------------------
     cell1 | cell4 | cell7
    -----------------------
     cell2 | cell5 | cell8
    -----------------------
     cell3 | cell6 |
    -----------------------

I think it would work if I could somehow draw a line on the bottom of an invisible UICollectionView header, and the same on the UICollectionView footer (a line on the top). An then for each UICollectionViewCell I could draw the missing lines.

However I don't know how to do this, and could not find anything on the subject that worked (somewhat) the same.

Upvotes: 1

Views: 3471

Answers (2)

Mundi
Mundi

Reputation: 80271

You could put a view representing the line in your cell subclass. That is not the best performing solution, but sufficient in most cases, plus you do not need to write any code. In your datasource method you could then set this line as hidden or not based on the index path.

E.g., if you do not want a line on the right if it is the third cell in a row, do something like

cell.rightBorder.hidden = indexPath.item % 3 == 0

Upvotes: 1

Rengers
Rengers

Reputation: 15238

You can achieve this by subclassing the cell and overriding drawRect. There you can draw a bottom and right border.

Upvotes: 0

Related Questions