CALayer: addSublayer() method
The addSublayer() method adds a layer as a sublayer, on top of the others.
Syntax
addSublayer(layer)Parameters
layer
A layer: CALayer, CAGradientLayer, or CAShapeLayer.
Return value
None (undefined).
- After it is added,
layeris at the end ofsublayersand is drawn above the other sublayers. To put it somewhere else, useinsertSublayer(). - A layer can have only one superlayer. If
layeris already under another layer, it is first removed from its previous superlayer. Not verified in the simulator. - Once added, the superlayer keeps it, and it stays on screen until it is removed, or the view its superlayer belongs to is removed.
Exceptions
TypeError
layer isn't a layer: addSublayer: expects a CALayer.
Examples
js
const host = new CALayer();
const a = new CALayer();
const b = new CALayer();
host.addSublayer(a);
host.addSublayer(b);
console.log(host.sublayers.indexOf(b));Result: The console prints 1. b is above a.
Corresponding iOS API
-[CALayer addSublayer:].