ios - UICollectionView 布局更新 - 项目大小不变

标签 ios uicollectionview uiinterfaceorientation

我对 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/

相关文章:

iphone - 根据方案包含/排除资源

ios - UICollectionView 在 insertSections 上崩溃,endItemAnimationsWithInvalidationContext :tentativelyForReordering:

drawrect - 使用drawRect绘制的collectionView :cellForItemAtIndexPath mixes up images,:

ios - 将 iPad 设置为仅在 LandscapeRight 中启动。

objective-c - 即使在 alloc/init 之后,我的 NSMutableArray 也会在函数范围之外丢失它的对象

ios - 将多个图像保存到 Parse

ios - 如何获取我的 iOS 应用程序中捆绑的字体名称?

ios - UICollectionViewController 在滚动时重新加载图像

ios - 如何知道 iOS 8 中的当前 interfaceOrientation?

ios - 如何根据开关按钮打开和关闭来更改设备的方向?