Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bringSubviewToFront for CALayer

Tags:

iphone

calayer

I'm wondering if there is a way to bring layer on top of others layers of view. Something like bringSubviewToFront does for UIView class. I think it can be done with zPosition property of the CALayer but this means I have to check zPosition for all layers and then set proper value.

Thanks in advance.

like image 497
Dmytro Avatar asked Oct 27 '09 08:10

Dmytro


2 Answers

I believe that the code (given that layer is your CALayer)

[layer retain];
CALayer *superlayer = layer.superlayer;
[layer removeFromSuperlayer];
[superlayer addLayer:layer];
[layer release];

will do what you want, albeit in a roundabout way.

like image 176
ianh Avatar answered Oct 03 '22 21:10

ianh


It's even easier. If sublayerToBeMovedToFront is the layer you're moving and itsSuperLayer is, well, its superlayer, just say:

[itsSuperLayer addSublayer:sublayerToBeMovedToFront];

The addSublayercall simultaneously unhookssublayerToBeMovedToFront from wherever it was in the sibling list and rehooks it as the last (ie, frontmost-positioned, visually "on top") sublayer. This is exactly analogous behaviour to that of [aUIView addSubview:]

like image 29
hkatz Avatar answered Oct 03 '22 19:10

hkatz