UICollectionView custom layout header view

Viewed 17

Frank. I read your blog about custom UICollectionViewlayout, it's awesome. I am encountering a problem with this topic for a few days. On a single viewcontroller, there are two buttons at the navigation title view and a collection view at the view of this ViewController. When clicking any button, the collectionView will reloadData and show the data from the server. There is a header view at the top of the collection view when clicking the first button, but for the second button, this header view should be not shown. The issue is when I click the second button, there will be an empty white header view at the top, even if I set the size equal to zero and zIndex to 0.

Code: https://cmd.im/dnan

1 Answers

Finally, I solve this problem. I am sure this is a bug in UIKit framework. The key point is the zIndex or transform3D of UICollectionViewLayoutAttributes can not be change after be set first time. So, I must use a indirect solution to solve it.

if xxx {
    cell.layer.zPosition = 100
} else {
    cell.layer.zPosition = 0
}

Set the zPosition of the layer in cells appearing when clicking the first button to more than 1, others to 0.

I don't know why this bug hasn't be solved until 2022. I used about 2 days to solve it. I am so disappointed.

Related