How do I bring a CALayer sublayer to the front of all sublayers, analogous to -[UIView bringSubviewToFront]?
Create a category of CALayer like this:
@interface CALayer (Utils)
- (void)bringSublayerToFront;
@end
@implementation CALayer (Utils)
- (void)bringSublayerToFront {
CGFloat maxZPosition = 0; // The higher the value, the closer it is to the front. By default is 0.
for (CALayer *layer in self.superlayer.sublayers) {
maxZPosition = (layer.zPosition > maxZPosition) ? layer.zPosition : maxZPosition;
}
self.zPosition = maxZPosition + 1;
}
@end