Search code examples
iosswiftuicollectionviewcgrectmake

Setting UICollectionView frame at runtime


I am trying to set the frame of my UICollectionView at runtime.

I tried using

mainCollectionView.frame = CGRect(x: 20, y: 20, width: self.view.frame.width-20, height: self.view.frame.width-20)

in my viewDidLoad but unfortunately it will always stay as in IB. I am not yet using constraints.

Otherwise resizing my cells is working in cellForItem:

 collectionCell.frame.size.width = self.view.frame.width/8 * 3

Solution

  • Try to set frame in viewDidLayoutSubviews