Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set collectionView size. (sizeForItemAtIndexPath function is not working) Swift 3

I have a tableView and in every tableViewCell is a collectionView.

I am trying to change the collectionView size with this code:

 func collectionView(_ collectionView: UICollectionView,                     layout collectionViewLayout: UICollectionViewLayout,                     sizeForItemAt indexPath: IndexPath) -> CGSize {      if collectionView.tag == 2{          return CGSize(width: 100, height: 100)      }else if collectionView.tag == 4 {           return CGSize(width: 222, height: 200)      }else{           return CGSize(width: 10, height: 10)     }  } 

The problem is that there is no error but cells do not change sizes

If you need to know something more, just write a comment.

Thank you.

like image 345
0ndre_ Avatar asked Oct 13 '16 11:10

0ndre_


People also ask

How do I change the size of a cell in collectionView?

class MyLayout: UICollectionViewFlowLayout { override func prepare() { super. prepare() guard let collectionView = collectionView else { return } itemSize = CGSize(width: ..., height: ...) } }

What is UICollectionViewFlowLayout?

A layout object that organizes items into a grid with optional header and footer views for each section.

What is compositional layout in Swift?

Overview. A compositional layout is a type of collection view layout. It's designed to be composable, flexible, and fast, letting you build any kind of visual arrangement for your content by combining — or compositing — each smaller component into a full layout.


1 Answers

First, don't forget to extends from UICollectionViewDelegateFlowLayout delegate.

For single collectionview display logic :

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {     let width = ((collectionView.frame.width - 15) / 2) // 15 because of paddings     print("cell width : \(width)")     return CGSize(width: width, height: 200) } 

And important point in storyboard don't forget Estimate Size : None

estimate_size_none

like image 132
Burak Dizlek Avatar answered Sep 23 '22 02:09

Burak Dizlek