Instance Method


Tells the delegate a layer's bounds have changed.


optional func layoutSublayers(of layer: CALayer)



The layer that requires layout of its sublayers.


The layoutSublayers(of:) method is called when a layer's bounds have changed and its sublayers may need rearranging, for example by changing its frame's size. You can implement this method if you need precise control over the layout of your layer's sublayers.

Listing 1 shows how you can create a class named LayerDelegate that implements CALayerDelegate and sets it as a layer's (named sublayer) delegate. When the layer's size changes, the delegate's layoutSublayers(of:) iterates over all of the sublayers of sublayer and resizes them to fit within it.

Listing 1

Laying out a layer's sublayers.

let delegate = LayerDelegate()
lazy var sublayer: CALayer = {
    let layer = CALayer()
    layer.sublayers?.first?.backgroundColor =
    layer.delegate = self.delegate
    return layer
// sublayer.frame = CGRect(x: 0, y: 0, width: 510, height: 510)
class LayerDelegate: NSObject, CALayerDelegate {
    func layoutSublayers(of layer: CALayer) {
        layer.sublayers?.forEach {
            $0.frame = layer.bounds