views:

462

answers:

3

I have a UIView which has about 8 different CALayer sublayers added to its layer. If I modify the view's bounds (animated), then the view itself gets shrinked (I checked it with a backgroundColor), but the sublayers' size remains unchanged.

How to solve this? I've just started googleing but haven't found any useful. Thanks in advance.

+2  A: 

Since CALayer on the iPhone does not support layout managers, I think you have to make your view's main layer a custom CALayer subclass in which you override layoutSublayers to set the frames of all sublayers. You must also override your view's +layerClass method to return the class of your new CALayer subclass.

Ole Begemann
Override +layerClass like in the case of OpenGL layer override? Think so.Set the frames of the sublayers? Set to what? I have to calculate all the sublayers' frames/positions individually depending on the superlayers frame size?Isn't there any "constraint-like", or "link-to-superlayer"-like solution? Oh, god, another day out of time-frame. CGLayers got resized, but was too laggy, CALayers are fast enough, but did not get resized. So many surprise. Thanks for the reply, anyway.
Geri
CALayer on the Mac has layout managers for this but they are not available on the iPhone. So yes, you have to calculate the frame sizes yourself. Another option could be to use subviews instead of sublayers. Then you could set the subviews' autoresizing masks accordingly.
Ole Begemann
Ya, UIViews are too performance expensive classes, thatswhy I use CALayers.
Geri
I tried the way you suggested. It works, and it is not. I resize the view animated, but the sublayers resizes in a different animation. Bloody hell, what to do now? What is the method to override in the CALayer subclass what invokes on EVERY (!) frame of the view's animation? Is there any?
Geri
Just bumped into this as well. Seems like the best option is to subclass CALayer as Ole said, but it seems unnecessarily cumbersome.
Dimitri
A: 

I had the same problem. In a custom view's layer I added two more sublayers. In order to resize the sublayers (every time the custom view's boundaries change), I implemented the method "layoutSubview" of my custom view; inside this method i just update each sublayer's frame to match the current boundaries of my subview's layer. Something like this:

-(void)layoutSubview{
   //keep the same origin, just update the width and height
   if(sublayer1!=nil){
      sublayer1.frame = self.layer.bounds;
   }

}
Solin