SelectedBackgroundView from UICollectionViewCell is displayed when it shouldn't be
I have a UICollectionView. With some cells inside with a white background color. I have set selectedBackgroundView to base purple view.
My CollectionView has a limit with a height of 0, and when I click the button, I update the limit to 80. When I do this, during the animation, I can see the purple background on the screen all the way to the end on and I can't figure out why or how to prevent it ? Everything else works fine, it's just a "visual" error. Any suggestion on how to fix this?
Gif error where you can see purple appearing during animation
Here is my cell design, if it can 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
}
source to share
- Subclassing your UICollectionViewCell
-
Do
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { // your code cell.selectedBackgroundView.hidden = true return cell }
-
Then in your subclass
override var selected:Bool { willSet { self.selectedBackgroundView.hidden = false } }
It should work.
source to share
This code appears to be executing in animation, resulting in unexpected timing behavior based on how various properties animate. Another complicating factor is that since the cells are reused it will not play if the reused cell is already set up correctly (i.e. nothing to animate). Adding the following after styling selectedBackgroundView
was the least dangerous solution I could think of.
[cell.selectedBackgroundView.layer removeAllAnimations];
Depending on your cells, you might also consider removing animations at other levels. For example:
[cell.backgroundView.layer removeAllAnimations];
source to share