Search code examples
iphoneobjective-ccore-animationcalayer

how do i get a layer's frame to automatically resize based on its superlayer's frame or its view's frame?


I'm experimenting with using cagradientlayer to draw gradients in our app instead of having a subclass of uiview manage gradients. One snafu that i've come across is that when the view that has the gradient as a sublayer of its main layer gets resized to fit the data i am trying to show, the layer doesn't resize along with it. I end up having the gradient layer end at the original frame size while my view's frame is much larger.

Is there a way to have the sublayer autoresize to fit its superlayer's frame, or the superlayer's view's frame?


Solution

  • Implement layoutSubviews in the subclass of your view. It gets called when the frame is resized (or when setNeedsLayout is called). Just set the layer's frame to the view's bounds:

    -(void)layoutSubviews
    {
        someSubview.frame = self.bounds;   // make the subview frame match its view
    }