Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Heres my 2 cents - because your item sizes are static why don't you set the item size on the <code>collectionViewLayout</code>?</p> <p>It will be quicker to do this rather than expecting the collection view to call its delegate method for every cell.</p> <p><code>viewWillLayoutSubviews</code> can be used for detection of rotation on view controllers. <code>invalidateLayout</code> can be used on a collection view layout to force it to prepare the layout again causing it to position the elements with the new sizes in this case.</p> <pre><code>- (void)viewWillLayoutSubviews; { [super viewWillLayoutSubviews]; UICollectionViewFlowLayout *flowLayout = (id)self.firstCollectionView.collectionViewLayout; if (UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication.statusBarOrientation)) { flowLayout.itemSize = CGSizeMake(170.f, 170.f); } else { flowLayout.itemSize = CGSizeMake(192.f, 192.f); } [flowLayout invalidateLayout]; //force the elements to get laid out again with the new size } </code></pre> <p><strong>edit: updated with Swift2.0 example</strong></p> <pre><code>override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else { return } if UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation) { flowLayout.itemSize = CGSize(width: 170, height: 170) } else { flowLayout.itemSize = CGSize(width: 192, height: 192) } flowLayout.invalidateLayout() } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload