I'm a beginner don't mind my coding nor my english I hope you can help me with this problem that you even might find it silly. I have been facing a problem with resizing the cell. I saw a few youtube tutorials and still couldn't find how to solve it!!
I also tried and changed it in the cell (the book picture is actually a button)
and here is the output. so how can I resize it? I want it to be 2 columns
here is the code
class resViewViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 140, height: 180)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return resourcesName.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collection.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! resourceCellCollectionViewCell
cell.name.text = resourcesName[indexPath.row]
return cell
}
I think the class you are looking for is the UICollectionViewDelegateFlowLayout
. Your UICollectionView
has a delegate and the delegate can implement the UICollectionViewDelegateFlowLayout
protocol. Inside that implementation you can specify the size of the cells by implementing
collectionView(_:layout:sizeForItemAt:)
So I would expect your code above to read:
class resViewViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 140, height: 180)
}
Note that the class is implements the UICollectionViewDelegateFlowLayout
protocol instead of just UICollectionViewDelegate
Then, in your Layout, you need to set the autoresizing mask of the view contained in the cell so that it shrinks or grows with the cell itself