CollectionView background clearColor not working

2020-05-22 15:13发布

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.

11条回答
我想做一个坏孩纸
2楼-- · 2020-05-22 15:41

To have a nice semi-transparent white background use:

collectionView.backgroundColor = UIColor(displayP3Red: 1.0, green: 1.0, blue: 1.0, alpha: 0.35)
查看更多
贼婆χ
3楼-- · 2020-05-22 15:42

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];
查看更多
祖国的老花朵
4楼-- · 2020-05-22 15:45

Swift 4.0 from Fogmeister's answer

self.collectionView.backgroundColor = UIColor.clear
self.collectionView.backgroundView = UIView.init(frame: CGRect.zero)
查看更多
Juvenile、少年°
5楼-- · 2020-05-22 15:49

In swift this work with me:

self.collectionView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.0)
查看更多
萌系小妹纸
6楼-- · 2020-05-22 15:58

I solved it using in Swift 3:

collectionViewVideo.backgroundColor = UIColor.clear.withAlphaComponent(0)
查看更多
登录 后发表回答