Elliott D'Alvarez
Elliott D'Alvarez

Reputation: 1237

UIImageView layer order

I am having trouble understanding how layering is performed within Xcode. I have a .xib with a view, that view has 6 UIImageView 's which each contain a different image. I have created IBOutlets for each of these so I can change the image at runtime.

I now want to change the order the UIImageViews are drawn at runtime, so imageA at the back is now drawn on top of everything else. How can I change the layer order? I originally tried removing all the subviews and using [self.view insertSubview: atIndex: ] to see if I could change the draw order that way but my code crashes.

Can anyone tell me the correct method for doing this please?

Thanks in advanced, Elliott

Upvotes: 1

Views: 3299

Answers (2)

meronix
meronix

Reputation: 6176

i guess you are looking for these UIView methods:

1: exchangeSubviewAtIndex:withSubviewAtIndex: Exchanges the subviews at the specified indices.

- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2

2: bringSubviewToFront: Moves the specified subview so that it appears on top of its siblings.

- (void)bringSubviewToFront:(UIView *)view

3: sendSubviewToBack: Moves the specified subview so that it appears behind its siblings.

- (void)sendSubviewToBack:(UIView *)view

with these methods you can exchange the subviews order to get one UIView appear over one other in their superview... if that's what you were asking....

Upvotes: 3

smparkes
smparkes

Reputation: 14053

A crash may be because when you remove the image from its parent, its retain count goes to zero and is deallocated.

If you just want to swap two views, see - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2 on UIView. If you want to do a more complicated exchange, you can remove and readd, just make sure you retain the view you remove (ARC notwithstanding).

Upvotes: 1

Related Questions