3

When I add a subview below a UICollectionView, sometimes the subview shows up above the UICollectionView cells. Here is the subview insertion code:

[self.collectionView insertSubview:self.garmentView atIndex:0];

Not sure if I'm not following a best practice or otherwise missing something obvious. Any assistance appreciated.

** Edit ** It might be worth noting that this only happens in landscape, when the rightmost cell is zoomed in.

Michael Mangold
  • 1,741
  • 3
  • 18
  • 32
  • Do you have custom view for each collection cell? – sissichen Aug 17 '14 at 21:12
  • Each cell has a UIImage and a UILabel. – Michael Mangold Aug 17 '14 at 21:29
  • 1
    When you say below, do you mean outside the collection view? If so, you should use a UIViewController with a collection view as one of the subviews. A UICollectionViewController (like a UITableViewController) doesn't have any other view, other than the collection view, so you can't add subviews that don't become part of the collection view. – rdelmar Aug 17 '14 at 21:52
  • Thanks for responding. I'm using a UICollectionViewController subclass that makes it easier to hook into Core Data, and I'd rather not deviate if I can avoid it. Please see my edit above in case that helps. Much appreciated, your idea does make sense. – Michael Mangold Aug 19 '14 at 02:10
  • Subclassing `UICollectionViewController` makes sense. Your edit could suggest that you need to do more work in the `prepareForReuse` method in your `UICollectionViewCell` subclass. – Lev Landau Aug 19 '14 at 10:24

2 Answers2

9

I think

self.garmentView.layer.zPosition = -1
[self.collectionView insertSubview:self.garmentView atIndex:0];

will solve your problem. My guess is that it can happen that the collection view cells gets added with a lower index than your garmentView. See this question for a more thorough discussion about subview positions.

Community
  • 1
  • 1
Lev Landau
  • 788
  • 3
  • 16
  • Thanks for responding. The garmentView now appears below the collectionView cell, but clicking on the cell still doesn't work. – Michael Mangold Aug 17 '14 at 21:27
  • I would try `self.garmentView.userInteractionEnabled = YES` in case it intercepts and blocks the touch events. If the problem persists I would investigate by putting a symbolic breakpoint in `[UIResponder touchesBegan:withEvent:]` and try to track what happens to the touch down event. – Lev Landau Aug 17 '14 at 21:49
  • I NSLogged self.garmentView.userInteractionEnabled at the relevant moment and it is YES. – Michael Mangold Aug 19 '14 at 02:12
0

Why not just:

[self.collectionView addSubview:self.garmentView];
[self.collectionView insertSubview:self.garmentView belowSubview:self.collectionView];

Are you using storyboard? What member of class is your controller?

Neru
  • 679
  • 6
  • 19