Heres my 2 cents - because your item sizes are static why don't you set the item size on the collectionViewLayout
?
It will be quicker to do this rather than expecting the collection view to call its delegate method for every cell.
viewWillLayoutSubviews
can be used for detection of rotation on view controllers.
invalidateLayout
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.
- (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
}
edit: updated with Swift2.0 example
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()
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…