【问题标题】:UICollectionView - cell's background color on iOS 6UICollectionView - iOS 6 上单元格的背景颜色
【发布时间】:2014-06-29 04:12:26
【问题描述】:

我在为 UICollectionViewCellUICollectionReusableView 对象设置背景颜色时遇到问题。它在 iOS 7 上运行没有问题,但在 iOS 6 上我得到的只是白色背景。

在我UICollectionView的数据源中,我在这个方法中设置了背景颜色:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

此代码仅适用于 iOS 7:

cell.contentView.backgroundColor = [UIColor redColor];

这是我已经尝试让它在 iOS 6 上运行的内容:

UIColor *backgroundColor = [UIColor redColor];
cell.backgroundColor = backgroundColor;
cell.backgroundView = [[UIView alloc] initWithFrame:cell.bounds];
cell.backgroundView.backgroundColor = backgroundColor;
cell.layer.backgroundColor = [backgroundColor CGColor];
cell.backgroundView.layer.backgroundColor = [backgroundColor CGColor];
cell.contentView.layer.backgroundColor = [backgroundColor CGColor];

不幸的是,没有成功。我很困惑,真的不知道我在这里错过了什么。

除了这个问题,其他一切都按预期工作,我的UICollectionView 在 iOS 7 和 iOS 6 上都能正确显示单元格。

我使用的代码可以在 GitHub 上找到,作为我制作的库的一部分。如果您需要有关我的实现的更多信息,请在此处查看:DRCollectionViewTableLayout-iOS。该存储库包含带有 UICollectionView 的演示项目。在 iOS 7 上,单元格具有随机颜色,在 iOS 6 上,所有单元格都有白色背景。

【问题讨论】:

    标签: ios ios6 uicollectionview uicollectionviewcell


    【解决方案1】:

    试试这个:

    [cell setBackgroundColor:[UIColor redColor]];
    

    更新

    cell.contentView.backgroundColor = [UIColor redColor];
    

    【讨论】:

    • 据我所知,这与cell.backgroundColor = [UIColor redColor]; 完全相同,但会试一试。
    • 不。这不会改变任何东西,iOS 6 上仍然是白色背景。
    • 更新了我的答案,希望对您有所帮助。
    • 我用来设置背景颜色的代码是在UICollectionView的数据源中实现的。在那种情况下,self 不是UICollectionViewCell,而是我的视图控制器(或其他实现UICollectionViewDataSource 协议的东西)。这不是答案。
    • 请再次阅读我的帖子,因为我已经尝试在单元格对象和单元格的 contentView 属性上设置 backgroundColor。
    【解决方案2】:

    解决了。这完全是我的错。我的UICollectionView 中的每个单元格和补充视图都有一个UILabel,它填充了它的框架。在 iOS 7 上 UILabels 默认有清晰的背景色,但在 iOS 6 上,默认背景色是白色。因此,我能够使用这种方法在 iOS 6 上正确设置背景颜色:

    cell.contentView.backgroundColor = [UIColor redColor];
    

    但是因为单元格顶部有一个白色背景的UILabel,所以单元格的背景颜色不可见。愚蠢的错误,但我认为最好记住,当您以编程方式创建新的UILabel 时,在 iOS 7 上它将有清晰的背景,但在 iOS 6 上它将是白色背景。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-13
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 2022-12-12
      • 2015-11-29
      • 1970-01-01
      相关资源
      最近更新 更多