ios CollectionView 背景 clearColor 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16570309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CollectionView background clearColor not working
提问by nebuto
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 :
我正在开发一个小的 collectionview 'framework' 来表现得像 iPad 上的浏览器标签栏(想想 chrome)。代码全部完成,自定义流程布局,重新排序等,并按如下方式组织:
? 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).
? TabBarCollectionViewController .h/.m/.xib 包含集合视图的高级逻辑(委托 + 数据源方法)。我有 xib 来配置 collectionView 设置并设置自定义流布局(我可以以编程方式执行此操作,但是这样更容易)。
? CustomFlowLayout .h/.m (subclass of flow layout)
? CustomFlowLayout .h/.m(流布局的子类)
? TabBarCell .h/.m/.xib (subclass of collectionviewcell)
? TabBarCell .h/.m/.xib(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.
然后我将 TabBarCVC 添加为我的主视图控制器上的子视图控制器(这个视图控制器有许多子视图控制器和子视图),然后作为子视图。在这一点上,一切都在工作。
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.
现在的问题,太愚蠢了,我不敢相信我还没有找到办法做到这一点,collectionView 的 backgroundColor 不能设置为 clearColor。我可以把它放在灰色或任何颜色,但它不支持透明度。单元格背景颜色也很清晰并且确实有效。
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.
我需要 collectionView 是透明的,以在后面的主视图上显示纹理。任何见解将不胜感激,或者也许我会填补我对苹果的第一个雷达。
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.
如果我找不到任何解决方案,我只会添加应该在 collectionView 后面的纹理的“屏幕截图”,并将其添加为 collectionView 的 backgroundView 中的 imageView。
采纳答案by nebuto
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...
好的,所以我现在感觉很愚蠢。我留下了一个空的 UIView,作为测试的 collectionView 的容器。我只是忘了删除它,现在一切正常,使用一个漂亮的 clearColor ......
回答by Kyle Robson
回答by Fogmeister
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];
回答by DaniEll
Watch out when setting UICollectionViews Background Color in Storyboard:
在 Storyboard 中设置UICollectionView的背景颜色时要注意:
The initially selected value Defaultis Black(counterintuitively).
You have to explicitly select Clear Colorfor the View to be transparent.
最初选择的值默认为黑色(违反直觉)。
您必须明确选择清除颜色以使视图透明。
Also note that the Preview in Storyboard immediately changes when this is done 'right'...
另请注意,当“正确”完成此操作时,Storyboard 中的预览会立即更改...
回答by Dary
The easiest solution is just pick anything color in color picker to change collectionview background then turn the opacity to 0%.
最简单的解决方案是在颜色选择器中选择任何颜色来更改 collectionview 背景,然后将不透明度设置为 0%。
回答by Sarfaraz Khan
I solved it using in Swift 3:
我在 Swift 3 中使用解决了它:
collectionViewVideo.backgroundColor = UIColor.clear.withAlphaComponent(0)
回答by Fabian Lauer
Fogmeister's answerworked great. Adapted to Swift 3, it would be:
Fogmeister的回答效果很好。适应 Swift 3,它将是:
self.collectionView.backgroundColors = [NSColor.clear]
self.collectionView.backgroundView = NSView.init(frame: CGRect.zero)
回答by Maciej
To have a nice semi-transparent white background use:
要获得漂亮的半透明白色背景,请使用:
collectionView.backgroundColor = UIColor(displayP3Red: 1.0, green: 1.0, blue: 1.0, alpha: 0.35)
回答by Matías Contreras Selman
Swift 4.0 from Fogmeister's answer
雨燕4.0从Fogmeister的答案
self.collectionView.backgroundColor = UIColor.clear
self.collectionView.backgroundView = UIView.init(frame: CGRect.zero)
回答by mrfoxix
In swift this work with me:
迅速与我合作:
self.collectionView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.0)