title: Reordering UIView subviews
body: <p>In my app I am trying bring a subview to front, then put it back to its original layer position later. The code should be pretty simple:</p>
  
  <p>To bring the subview to front (inside my custom UIView class):</p>
  
  <pre><code>[self.superview bringSubviewToFront:self];
  </code></pre>
  
  <p>Easy. I store the original z position in an instance variable called, you guessed it, <code>zPosition</code>. So, the line before <code>-bringSubviewToFront:</code> is:</p>
  
  <pre><code>zPosition = [self.superview.subviews indexOfObject:self];
  </code></pre>
  
  <p>So, all of the code I use to bring my subview to front is:</p>
  
  <pre><code>zPosition = [self.superview.subviews indexOfObject:self];
  [self.superview bringSubviewToFront:self];
  </code></pre>
  
  <p>This works as it should. The problem is when I try to put the subview back where it was. I'm simply doing this:</p>
  
  <pre><code>[self.superview exchangeSubviewAtIndex:zPosition withSubviewAtIndex:
      [self.superview.subviews indexOfObject:self]];
  </code></pre>
  
  <p>Using this code, if I have two subviews, this is what happens:</p>
  
  <p>Let's say I have view A and view B. View A is above view B. I tap view B, it comes to the front. I tap view B again (it should go back to where it was), and nothing happens, so it's now on view A. If I now tap view A, it comes to the front, but when I tap it again (so it should go back to its original z position: below view B), all of its sibling views disappear!</p>
  
  <p>Does anyone see what could be causing this problem?</p>