Add border to custom CollectionView

Aнгел picture Aнгел · May 16, 2015 · Viewed 16.4k times · Source

How can I add border to UICollectionView's cells which uses a custom UICollectionViewFlowLayout? When I override UICollectionView's flow layout, cell borders are "removed". how can I set borders properly? Thank you!

Answer

skim picture skim · May 16, 2015

I'm not sure how you want your border to look like, but whenever I need a quick border around a view, I usually use the following:

var view = UIView(frame: frame)
view.layer.borderWidth = 1
view.layer.borderColor = UIColor.blackColor().CGColor

Update Swift 3

view.layer.borderColor = UIColor.black.cgColor

You can either probably apply this to your collection view cell or create a custom class that sets these values after initialization.

Either that, or you can set the cell size so that spaces between cells serve as borders and set minimumLineSpacing and minimumInteritemSpacing in your custom flow layout implementation.