Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

xArtx

macrumors 6502a
Original poster
Mar 30, 2012
764
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?
 

xArtx

macrumors 6502a
Original poster
Mar 30, 2012
764
1
For optimal resource management, I would recommend only having visible views attached to any other view, and otherwise just sitting in a strong pointer.

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.
 

ConCat

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

Punkjumper

macrumors member
Jan 12, 2013
48
3
Code:
insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;

bringSubviewToFront:(UIView *)view;
sendSubviewToBack:(UIView *)view;
 

xArtx

macrumors 6502a
Original poster
Mar 30, 2012
764
1
Code:
insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;
insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;

bringSubviewToFront:(UIView *)view;
sendSubviewToBack:(UIView *)view;

Nice :)
Looks like two different ways, not just one lump of code.
 

ctdonath

macrumors 68000
Mar 11, 2009
1,592
629
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];
 
Last edited:

KoolStar

macrumors demi-god
Oct 16, 2006
825
9
Kentucky
Probably more like setNeedsDisplay, but maybe they do the same thing.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.