views:

252

answers:

1

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?

+1  A: 

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
}
progrmr
I'm hoping to not need a subclass at all, but some functionality that may exist in CALayers that i can't seem to find that would do this for me (subclassing a layer is not out of the question though).
Kevlar
How are you resizing the view? I resize views by setting the frame and have never had to resize the layers.
progrmr
i'm setting the frame in some cases, and letting the view get autoresized in others. i think the problem might be that when i resize the views manually i turn off autoresizing of subviews so that the subviews themselves don't resize; that might be my problem.
Kevlar