I am trying to implement a CollectionView
.
When I am using Autolayout, my cells won't change the size, but their alignment.
Now I would rather want to change their sizes to e.g.
//var size = CGSize(width: self.view.frame.width/10, height: self.view.frame.width/10)
I tried setting in my CellForItemAtIndexPath
collectionCell.size = size
it didn't work though.
Is there a way to achieve this?
edit:
It seems, that the answers will only change my CollectionView width and height itself. Is there are conflict in Constraints possible? Any ideas on that ?
You have 2 ways in order to change the size of CollectionView.
First way -> add this protocol UICollectionViewDelegateFlowLayout
for In my case I want to divided cell into 3 part in one line. I did this code below
Second way -> you don't have to add UICollectionViewDelegateFlowLayout but you have to write some code in viewDidload function instead as code below
Whatever you write a code as the first way or second way you will get the same result as above. I wrote it. It worked for me
An other way is to set the value directly in the flow layout
Size ratio according to iPhone size :
Here's what you can do to have different width and height for cell's regarding the iPhone size :
And maybe you should also disable your AutoLayout constraints on cell for this answer to work.
This is my version, find your suitable ratio to get the cell size as per your requirement.
swift4 swift 4 ios collection view collectionview example xcode latest code working sample
Add this on Delegate section of the top
UICollectionViewDelegateFlowLayout
and use this function
///// sample complete code
create on collection view and collectionview cell in storyboard give reference to the collection as
@IBOutlet weak var cvContent: UICollectionView!
paste this in View controller
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
}
The collection view has a layout object. In your case, it is probably a flow layout (UICollectionViewFlowLayout). Set the flow layout's
itemSize
property.