I have an UICollectionView inside of an UIViewcontroller. I want to know how to set a maximum of cells in each row.
This question was asked before here: UICollectionView display 3 items per row
This is answered however in an Objective C. Therefore I can not understand the answer. Also, this is done programmatically. Is there a way to do this in the interface builder? Edit: 3 people reported this as a duplicate. Yes, of course it's a duplicated question, I literately already mentioned that. Again: the questions are given in Objective C which I can not read. Therefore the answer of this topic should differ from the the topic I already mentioned.
So this is the case. I want to know if its possible to display 3 items in a row. So if there are 4 items, the first row displays 3 cells and the second row displays the other cell. I tried it with autolayout, but this is not how it works I guess. The cell's size remains the same. This way the cell's size is the same on all devices. This is not what I want. The width and height of the cell should be proportional to the view's width and heights. Let say view.width * 0.25 and height view.height * 0.5.
Edit: I already tried this:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.size.width/4 - 0, height: collectionView.bounds.size.height/4 - 0)
}
And played with the number's (4 and 0) but not the result I wanted.
Edit: Found it out Final answer: if you want to have 3 rows, and make sure minimal spacing is higher than 0:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.size.width/4 - 0, height: collectionView.bounds.size.height/1.5)
}