Change the alpha of images in an array

Hi Everyone

I have an array with images in being displayed by a collection view. I am trying to use code to select one of the cells at random and change the alpha setting of the image so it disappears and revels what is behind it. I have been staring at the screen for days and my brain is melting. I have tried hundreds of techniques and nothing is working. Any advice would be appreciated. Relevant code is below. I am a new Swift comrade so apologies if this is simple.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! CollectionViewCell

            cell.yellowSquare.image = cellImages[indexPath.item]

            let randomcell = cellImages.randomElement()
            if randomcell == UIImage(named: "gridsquare") {
                cell.alpha = 0

Hi @JAC Welcome to the community

Try this:

cell.backgroundView.alpha = 0


cell.contentView.alpha = 0

I am not really sure which one you are after.


Sir, I cannot thank you enough. It worked, I now have a red square where was it was yellow and am well on my way.

For your kind welcome, your blessings, and your knowledge, I salute you.

Have an amazing day.