1

I'm searching for a way to let a CALayer resize itself whenever its sublayers change (which means either when the bounds of any sublayer change or when the sublayer array itself changes).

When i worked with views before, i managed that through implementing sizeThatFits in my custom UIView subclass, which was called automatically by sizeToFit whenever the view's subviews changed.

Since CALayer has the sizeThatFits-equivalent-method preferredSize, i was surprised not to find a sizeToFit-equivalent.

bg890
  • 11
  • 4
  • Take a look at this: http://stackoverflow.com/questions/8219588/catextlayer-wrapped-sizetofit – Carles Estevadeordal Feb 24 '12 at 12:52
  • 1
    i think that's about sizeThatFits, not sizeToFit, since he wants to know what the size of the layer should be. I'm able to calculate that size (via preferredSize), but i need a method which gets called whenever the subviews change (which then would call preferredSize and set the layer's frame accordingly). – bg890 Feb 24 '12 at 14:14
  • Why don't just use a UIView instead? – Carles Estevadeordal Feb 24 '12 at 17:12
  • 2
    CALayer just seems to fit better, i don't need touch handling, i get implicit animations and i don't have to bother about forwarding touches which were sent to it. Moreover the layer-version should be a bit faster. – bg890 Feb 24 '12 at 18:20

1 Answers1

0

I think you need to implement(override) the - (void)layoutSublayers ... *Subclasses can override this to * provide their own layout algorithm, which should set the frame of * each sublayer

- (void)layoutSublayers
{
 // self.frame = aNewSubLayer.bounds
// basically do algorithm for setting frame and bounds for this(self) layer and subLayers here
}
abdimuna
  • 775
  • 9
  • 16