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.
class MyLayout: UICollectionViewFlowLayout { override func prepare() { super. prepare() guard let collectionView = collectionView else { return } itemSize = CGSize(width: ..., height: ...) } }
A layout object that organizes items into a grid with optional header and footer views for each section.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With