我对 collectionView 的项目大小有疑问。我想在纵向模式下连续显示 3 个项目,在横向模式下显示 6 个项目。我已经像这样设置了 -layoutSubviews:
- (void)layoutSubviews {
[super layoutSubviews];
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)) {
//Portrait orientation
self.flowLayoutPortrait.itemSize = CGSizeMake(106, 106);
} else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight)) {
//Landscape orientation
self.flowLayoutPortrait.itemSize = CGSizeMake(93, 93);
}
[self.collectionView.collectionViewLayout invalidateLayout];
}
但单元格大小不会更新,它在两个方向上始终保持不变。 如果我创建 2 个 flowLayouts 并使用:
[self.collectionView setCollectionViewLayout:self.flowLayoutLandscape];
一切正常,但我真的不喜欢它们的变化方式。动画真的很糟糕。 因为 itemSize 是唯一需要更新的属性,所以使用 1 布局似乎是最佳选择。
如何告诉 collectionView 更新布局?
最佳答案
我使用了这个方法,这对我来说非常好:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (_isLandscape)
return CGSizeMake(yourLandscapeWidth, yourLandscapeHeight);
else
return CGSizeMake(yourNonLandscapeWidth, yourNonLandscapeHeight);
}
关于ios - UICollectionView 布局更新 - 项目大小不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23738499/