4

I've got a UICollectionView. With some cells inside with a white background color. I've set the selectedBackgroundView to a basic purple view.

My CollectionView has a constraint with a height of 0 and when I hit a button I update the constraint to 80. When I'm doing that, during the animation i can see the purple background on the screen until the end on the animation and i cannot understand why or how prevent this ? Everything else working fine, it's just a "visual" bug. Any suggestion about how to fix this ?

Gif of the bug where you can see the purple appearing during the animation Visual bug

Here is my cell construction if it can be of any help :

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("WidgetMenuCellIdentifier", forIndexPath: indexPath) as UICollectionViewCell

    cell.removeSubviews()

    // some code setup

    cell.selectedBackgroundView = UIView()
    cell.selectedBackgroundView.backgroundColor = UIColor.purpleColor()

    return cell
}
Loadex
  • 1,392
  • 1
  • 11
  • 25

2 Answers2

3
  1. Subclass your UICollectionViewCell
  2. Do

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
    // your code
    cell.selectedBackgroundView.hidden = true
    
    return cell
    }
    
  3. Then in your subclass :

    override var selected:Bool {
       willSet {
           self.selectedBackgroundView.hidden = false
       }
    }
    

It should work.

anasaitali
  • 1,474
  • 21
  • 30
0

It seems like this code is being executed within an animation, causing unexpected behavior at times based on how various properties animate. Another complicating factor is that, because cells are reused, it won't reproduce if a reused cell is already configured correctly (i.e. there is nothing to animate). Adding the following after styling the selectedBackgroundView was the least hacky solution I could think of.

[cell.selectedBackgroundView.layer removeAllAnimations];

Depending on what your cells are like you may also want to consider removing animations on other layers as well. For example:

[cell.backgroundView.layer removeAllAnimations];
mon4goos
  • 1,539
  • 13
  • 22