Cauchy Kun Cauchy Kun - 5 months ago 37
Objective-C Question

How to set opacity on UICollectionView background without affect opacity of UICollectionViewCell?

I have set the color of a

UICollectionView
with an
image
pattern like so:

self.collectionView.backgroundColor = [UIColor colorWithPatternImage:image];


I would like to set the alpha of the
collectionView
without affecting the alpha of the
UICollectionViewCell
. Is there a way to do this? Setting the alpha of
collectionView
also affects the
UICollectionViewCell
, so I already tried that. What else should I try/what else will actually work?

Thanks for any tips.

Answer

Use the -colorWithAlphaComponent: method of UIColor:

[self.collectionView setBackgroundColor:[[UIColor redColor] colorWithAlphaComponent:0.5]];

This will result in only the background having a non-1 alpha value.