0

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)
}
Andrea Mugnaini
  • 8,880
  • 3
  • 34
  • 49
  • 1
    FYI - The question you linked is not in an older version of Swift. It's in Objective-C. – rmaddy Jan 26 '17 at 17:41
  • Oops :( gonna edit it – Joe Dummings Jan 26 '17 at 17:44
  • maybe this can help? http://stackoverflow.com/a/40826741/6448167 OR http://stackoverflow.com/a/14675248/6448167 – KSigWyatt Jan 26 '17 at 17:59
  • Possible duplicate of [UICollectionView display 3 items per row](http://stackoverflow.com/questions/34116251/uicollectionview-display-3-items-per-row) – KSigWyatt Jan 26 '17 at 18:02
  • Yes KSigWyatt, I already mentioned it in my question. The answers are given in an other language. How could that possibly help me if I can not understand it? And thank you for redirecting me to the topics. However again is one answer in Objective C and the other answer did not help me. I updated the question and included a function that didn't work. – Joe Dummings Jan 26 '17 at 20:33
  • Edit: It worked after I changed some code xCode suggested. – Joe Dummings Jan 26 '17 at 21:02
  • If it works, post answer for others to read. – Will Boland Jan 26 '17 at 21:15

0 Answers0