How to set opacity on a background of a UICollectionView without affecting the opacity of a UICollectionViewCell?

I set the color of the UICollectionView with the image template as follows:

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

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

Thanks for any advice.

+7
ios objective-c
source share
2 answers

Use the -colorWithAlphaComponent: method of UIColor :

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

This will only cause the background to have a value other than 1 alpha.

+5
source share

If you want to change the opacity without changing the color, or you need to track the color itself, you can use:

 self.collectionView.backgroundColor = [self.collectionView.backgroundColor colorWithAlphaComponent:0.5f]; 

There are two possible โ€œbackgroundsโ€ in the UICollectionView that you can change the opacity. The backgroundColor property is the simplest, but only changes the full-color background. There's also a backgroundView , which may have subviews that won't change transparency if you just change the opacity of the background color. You can change the opacity of the backgroundView with:

 self.collectionView.backgroundView.alpha = 0.5f; 
+2
source share

All Articles