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

11,627

Solution 1

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()

Solution 2

using in Swift 3:

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

Solution 3

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];

Solution 4

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

Solution 5

In the UICollectionView set the background color to clearcolor in the attribute inspector.

Share:
11,627
laser2302
Author by

laser2302

Updated on June 28, 2022

Comments

  • laser2302
    laser2302 almost 2 years

    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?

  • Simon McLoughlin
    Simon McLoughlin over 8 years
    this does not answer the OP's question
  • laser2302
    laser2302 over 8 years
    There's an image behind the collection view that I want to be made visible behind the cells. If I make it white then image behind would still not be visible.
  • Simon McLoughlin
    Simon McLoughlin over 8 years
    @laser2302 you are not reading my answer. There are 2 controls in question here, the collectionView and its cells. The cells are by default set to clear. Set your collectionView to clear as you have been, then go to each of your cells and give them a background color, to overwrite the default setting of clear
  • laser2302
    laser2302 over 8 years
    Solved! I found out I was doing it wrong, I was setting it as Default instead of Clear Color. Thanks a lot buddy!
  • Chen Li Yong
    Chen Li Yong over 5 years
    @SimonMcLoughlin but this answers mine anyway.