UICollectionView not obeying zIndex of UICollectionViewLayoutAttributes

Updating UICollectionViewLayoutAttributes.zIndex doesn't work due to UIKit bug.

This code does the trick:

// In your UICollectionViewCell subclass:
override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
    super.apply(layoutAttributes)
    layer.zPosition = CGFloat(layoutAttributes.zIndex) // or any zIndex you want to set
}

Setting zIndex in UICollectionViewLayout didn't work for me

I set the zIndex inside the UICollectionViewController's method

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
customCell.layer.zPosition = 0;
}

My best guess is something between the layout and the controller sets the zIndex to something weird