UICollectionView layout change when frame size changes

1.7k views Asked by At

I'm working with a UICollectionView but I have a weird behaviour.

I want a horizontal scroll, small cell height ( smaller than the UICollectionView height). Tapping a button will increase even more the height of the collection.

The problem is that the layout of my cells is changing also. I want the collection view height to increase with the cells staying in the same position. Here attached 2 screen captures (before/after button touched). Here is my code:

var smallLayout:UICollectionViewFlowLayout = UICollectionViewFlowLayout();
smallLayout.scrollDirection = .Horizontal
var itemHeight = 100
smallLayout.itemSize = CGSizeMake(150, itemHeight);
var bottomDist:CGFloat = 1
var topDist = collHeight - itemHeight - bottomDist
smallLayout.sectionInset = UIEdgeInsets(top: topDist, left: 2, bottom: bottomDist, right: 2)
smallLayout.minimumInteritemSpacing = 15
smallLayout.minimumLineSpacing = 10

So, when touching the button I have the following code:

let viewHeight = CGRectGetHeight(self.view.frame)
let viewWidth = CGRectGetWidth(self.view.frame)
var collHeight:CGFloat = 600
var collYPosition = viewHeight-CGFloat(collHeight)-20
var rect = CGRectMake(0, CGFloat(collYPosition), CGFloat(viewWidth), CGFloat(collHeight))
dishCollectionView.frame = rect

So, I cannot understand why my layout is changing.

I want to have my cell at the bottom.

enter image description here enter image description here

1

There are 1 answers

0
Patrick Lynch On BEST ANSWER

Perhaps if you reset the top of your sectionInset to accommodate for the additional height, i.e. an updated topDist calculation. Try adding something like this to you on touch code:

var bottomDist:CGFloat = 1
var topDist = collHeight - itemHeight - bottomDist
dishCollectionView.collectionViewLayout.sectionInset = UIEdgeInsets(top: topDist, left: 2, bottom: bottomDist, right: 2)
dishCollectionView.collectionViewLayout.invalidateLayout()