我解决了使用两个UICollectionViewFlowLayout的问题。一种用于肖像,另一种用于风景。我在-viewDidLoad中将它们分配给我的collectionView
self.portraitLayout = [[UICollectionViewFlowLayout alloc] init];
self.landscapeLayout = [[UICollectionViewFlowLayout alloc] init];
UIInterfaceOrientation orientationOnLunch = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(orientationOnLunch)) {
[self.menuCollectionView setCollectionViewLayout:self.portraitLayout];
} else {
[self.menuCollectionView setCollectionViewLayout:self.landscapeLayout];
}
然后我像这样简单地修改了我的collectionViewFlowLayoutDelgate方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGSize returnSize = CGSizeZero;
if (collectionViewLayout == self.portraitLayout) {
returnSize = CGSizeMake(230.0, 120.0);
} else {
returnSize = CGSizeMake(315.0, 120.0);
}
return returnSize;
}
最后我轮流从布局切换到另一个
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if (UIInterfaceOrientationIsPortrait(fromInterfaceOrientation)) {
[self.menuCollectionView setCollectionViewLayout:self.landscapeLayout animated:YES];
} else {
[self.menuCollectionView setCollectionViewLayout:self.portraitLayout animated:YES];
}
}