简体   繁体   中英

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

I have a collection view and of course I also have the cells in it. If in the property inspector I change the alpha of collection view to 0, the cells in it also become transparent. Is there a way to make the background transparent of the collection view only so the image behind it is visible?

A UICollectionViewCell 's background is defaulted to clear. Simply set a background colour for your cells either in interface builder or in a subclass using:

self.backgroundColor = UIColor.WhiteColor()

在Swift 3中使用:

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

You can change the cell colour to clear and set the background to nil to see whats underneath the collectionView. Like this:

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

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

Dropping UICollectionView 's Opacity to 0 from attribute inspector will make it transperent.

PS:

Bad practice, but will work.

if you drop collection view opacity to 0 the whole collection view will be invisible. In your case you want to show collection view cell only so collection view cell's background view should be UIColor.clear either collection view background. Programmatically like this

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

In my case i need to show chat log like this so i used that code

my example case

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM