38

I'm developing a small collectionview 'framework' to behave like a browser tab bar (think chrome) on the iPad. The code is all done, custom flow layout, reordering, and so on and is organized as so :

• TabBarCollectionViewController .h/.m/.xib contains the high logic of the collection view (delegates + datasource methods). I have the xib to configure the collectionView settings and set the custom flow layout (I could do this programmatically, but oh well it's easier that way).

• CustomFlowLayout .h/.m (subclass of flow layout)

• TabBarCell .h/.m/.xib (subclass of collectionviewcell)

Then I'm adding the TabBarCVC as a childViewController on my main viewController (this viewController has many childViewController and subviews) and then as a subview. At this point all is working fiiiiine.

Now the problem, it's so stupid i can't believe i haven't found a way to do this, the backgroundColor of the collectionView is not settable to clearColor. I can put it in gray or whatever color, but that it doesn't support transparency. The cell background color is also clear and does work.

I need the collectionView to be transparent to show the texture on the main view behind. Any insight would be much appreciated, or perhaps i'll fill my first radar to apple.

If i can't find any solution i'll just add the 'screenshot' of the texture supposed to be behind the collectionView and add it as a imageView in the collectionView's backgroundView.

nebuto
  • 994
  • 2
  • 9
  • 16

11 Answers11

98

In my case I had the background color in the storyboard set to Default. This caused it to have a black background. Changing it to Clear Color worked.

enter image description here

Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
Kyle Robson
  • 3,060
  • 24
  • 20
50

Try setting the color to clear and the background view to an empty view like so...

self.collectionView.backgroundColor = [UIColor clearColor];
self.collectionView.backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • 5
    Thanks Fogmeister. Alternatively one can set collectionView.backgroundView.backgroundColor = [UIColor clearColor] – CodeBrew Jun 30 '15 at 16:20
  • 4
    Just use a nil background view. Do not bother with a blank one: self.collectionView!.backgroundView = nil – Joel Teply Nov 16 '15 at 21:45
  • 2
    @JoelTeply thanks, I'll let my two and a half year ago self know. – Fogmeister Nov 16 '15 at 21:55
  • @JoelTeply so should the answer get updated to use Joel's suggestion? – Crashalot May 22 '16 at 07:52
  • @Crashalot if my answer no longer works then the edit button is there for your convenience. – Fogmeister May 22 '16 at 07:55
  • never feel comfortable editing the substance of an answer without permission ... so will assume this confers permission and will edit :) – Crashalot May 22 '16 at 07:59
  • @Crashalot ok, so what you've edited will now be rolled back. My answer worked and you've removed the part of my answer that made it work. Joel's solution may also work but now my solution is gone. – Fogmeister May 22 '16 at 08:03
10

Ok so i'm feeling pretty stupid now. I left an empty UIView behind, acting as a container for the collectionView for a test. I simply forgot to remove it, all is working fine with a nice clearColor now...

nebuto
  • 994
  • 2
  • 9
  • 16
7

Watch out when setting UICollectionViews Background Color in Storyboard:

The initially selected value Default is Black (counterintuitively).
You have to explicitly select Clear Color for the View to be transparent.

Also note that the Preview in Storyboard immediately changes when this is done 'right'...

DaniEll
  • 1,022
  • 4
  • 22
  • 31
2

The easiest solution is just pick anything color in color picker to change collectionview background then turn the opacity to 0%.

Pengguna
  • 4,636
  • 1
  • 27
  • 32
1

I solved it using in Swift 3:

collectionViewVideo.backgroundColor = UIColor.clear.withAlphaComponent(0)
Sarfaraz Khan
  • 572
  • 7
  • 20
1

Fogmeister's answer worked great. Adapted to Swift 3, it would be:

self.collectionView.backgroundColors = [NSColor.clear]
self.collectionView.backgroundView = NSView.init(frame: CGRect.zero)
Fabian Lauer
  • 8,891
  • 4
  • 26
  • 35
1

Swift 4.0 from Fogmeister's answer

self.collectionView.backgroundColor = UIColor.clear
self.collectionView.backgroundView = UIView.init(frame: CGRect.zero)
Matias Contreras
  • 442
  • 5
  • 10
0

To have a nice semi-transparent white background use:

collectionView.backgroundColor = UIColor(displayP3Red: 1.0, green: 1.0, blue: 1.0, alpha: 0.35)
Mobile Developer
  • 5,730
  • 1
  • 39
  • 45
0

In swift this work with me:

self.collectionView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.0)
mrfoxix
  • 66
  • 5
0

What is did to fix it

From Storyboard set collection view background colour as clear colour

then set main view colour to any colour you want , (I set to white.)

Logic
  • 705
  • 1
  • 9
  • 27