0

I have a collection view with multiple cells in it, I figured out how to add a border to each cell but still stuck with adding a border to the CollectionView (expected Result).

Currently, I'm placing the button outside the CollectionView but don't know how to make it looks like the expected result.

Current view

Expected Result

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70

2 Answers2

1

For set the collection view border you can follow this

collectionView.layer.borderColor = UIColor.gray.cgColor  
collectionView.layer.borderWidth = 5  

And for setting cell position to the middle you need to set the cell insect. You can easily set the cell insect from storyboard from this. Update the Section Insects value that you want.

enter image description here

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
0

you can try like this:

@IBOutlet var CollectionView: UICollectionView!

 override func viewDidLoad() {
 super.viewDidLoad()

CollectionView.delegate = self
CollectionView.dataSource = self

CollectionView.layer.borderColor = UIColor.gray.cgColor //change to required color
CollectionView.layer.borderWidth = 5 //change to required borderwidth
}
  • Big thanks! I was dumb to not think about IBOutlet, I totally forgot that. Now i'm facing another problem that cells don't place in middle of the CollectionView https://imgur.com/8F5O2S3. Do you know what should I do? – AdorableMelon Apr 03 '19 at 04:55
  • @AdorableMelon https://stackoverflow.com/questions/35975507/set-insets-to-the-collectionview-programmatically-in-swift – RajeshKumar R Apr 03 '19 at 05:00
  • @AdorableMelon select collectionview in storyboard and click on show size inspector in section insets change top,buttom,left,right to required value – Santosh Kumar J M Apr 03 '19 at 05:25