Reordering UIImageView layers

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

  1. macrumors 6502a

    Joined:
    Mar 30, 2012
    #1
    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?

    Code:
        [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. macrumors 6502a

    ConCat

    Joined:
    Jul 27, 2012
    Location:
    In an ethereal plane of existence.
    #2
    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. thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #3
    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. macrumors 6502a

    ConCat

    Joined:
    Jul 27, 2012
    Location:
    In an ethereal plane of existence.
    #4
    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 stackoverflow.com 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. macrumors member

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

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

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

    macrumors 65816

    ctdonath

    Joined:
    Mar 11, 2009
    #8
    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. macrumors 68030

    PhoneyDeveloper

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

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #10
    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