如何在 Swift 中使 Collection 视图的背景透明而不使其中的单元格透明

How can I make the background of UICollection view as transparent without making the cells in it transparent in Swift

我有一个集合视图,当然我也有其中的单元格。如果在 属性 检查器中我将集合视图的 alpha 更改为 0,其中的单元格也会变得透明。有没有办法使集合视图的背景透明,以便它后面的图像可见?

A UICollectionViewCell的背景默认为透明。只需在界面构建器或子类中使用以下方法为您的单元格设置背景颜色:

self.backgroundColor = UIColor.WhiteColor()

您可以将单元格颜色更改为清除并将背景设置为 nil 以查看 collectionView 下面的内容。像这样:

  collectionview.backgroundView = nil;
  collectionview.backgroundColor = [UIColor clearColor];

从属性检查器中将 UICollectionView 的不透明度降低到 0 将使它变得透明。

P.S:

不好的做法,但会奏效。

在 UICollectionView 的属性检查器中将背景颜色设置为 clearcolor。

在 Swift 中使用 3:

collectionView.backgroundColor = UIColor.clear.withAlphaComponent(0)

如果将集合视图的不透明度降低到 0,整个集合视图将不可见。在您的情况下,您只想显示集合视图单元格,因此集合视图 单元格的 背景视图应该是 UIColor.clear 集合 view 背景。像这样编程

    self.collectionView.backgroundView?.backgroundColor = UIColor.clear
    self.collectionView.backgroundColor = UIColor.clear

在我的例子中,我需要像这样显示聊天记录,所以我使用了那个代码

my example case