Reordering UIImageView layers

Discussion in 'iOS Programming' started by xArtx, Jan 14, 2013.

  1. xArtx macrumors 6502a

    Mar 30, 2012
    Hi Guys,

    I have some situations where I want a UIImageView that is normally in the background,
    to become the front layer (it has a lot of transparent area).

    Is the best way to remove the view and then add it again in the right order?

        [backImageView removeFromSuperview]; // remove and add again to bring background to the front
        [frontImageView removeFromSuperview];       
        [self.view addSubview:frontImageView];
        [self.view addSubview:backImageView];
    Or should I create a duplicate of the back layer, put one in front,
    and one at the back at all times, and keep one or the other clear?

    I guess the question I'm asking is about the overhead for iOS in going
    one way or the other. Some part of iOS must have to draw this extra
    blank layer to the screen, even if we don't have to do it manually right?
  2. ConCat macrumors 6502a


    Jul 27, 2012
    In an ethereal plane of existence.
    For optimal resource management, I would recommend only having visible views attached to any other view, and otherwise just sitting in a strong pointer.
  3. xArtx thread starter macrumors 6502a

    Mar 30, 2012
    So the first way is fine if it's only being swapped while framerate doesn't matter?

    When the background is in front, the foreground layer is still visible thanks to transparency.
    Think a row of hills as the background, but you might want it to be a foreground
    if the objects on the other layer are going to appear to be behind the hills.
  4. ConCat macrumors 6502a


    Jul 27, 2012
    In an ethereal plane of existence.
    Ah, okay, I get what you're saying. I don't have much experience with that specific scenario. I was just commenting on something I knew.
    You might want to try though. You're almost guaranteed to get your questions answered there, assuming the question isn't too complex, which this isn't, I just don't personally know.
  5. Punkjumper macrumors member

    Jan 12, 2013
    insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
    insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;
    bringSubviewToFront:(UIView *)view;
    sendSubviewToBack:(UIView *)view;
  6. xArtx thread starter macrumors 6502a

    Mar 30, 2012
    Nice :)
    Looks like two different ways, not just one lump of code.
  7. Punkjumper macrumors member

    Jan 12, 2013
  8. ctdonath, Jan 15, 2013
    Last edited: Jan 15, 2013

    ctdonath macrumors 65816


    Mar 11, 2009
    Related question: how to prompt re-running of loadView: ? i.e.: having changed order/content of self.view, need to regenerate what's displayed.

    ETA: Ah, it's [self.view setNeedsLayout];
  9. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    Probably more like setNeedsDisplay, but maybe they do the same thing.
  10. KoolStar macrumors demi-god


    Oct 16, 2006
    If we look at what setNeedsLayout and setNeedsDisplay do we can see that setNeedsDisplay calls drawRect: at the end of the run loop. SetNeedsLayout do the same thing with layoutSubviews.

    Also setNeedsLayout will only have an effect if your subclass overrides layoutSubviews method. In most cases setNeedsDisplay should be sufficient to repaint a view.
    That may help some people out when they go to use either method call.

Share This Page