0

I've got a custom layer which is sub-layer of something else. The problem is, my layer can't know it's size until I'm in drawLayer:inContext:.

When I don't change the bounds/frame of my layer everything draws perfectly.

When I add in the the line to change frame/bounds, my background looks perfect, but my content doesn't exist. It looks like the content is clipping off the original bounds instead of the new (bigger) bounds so I see nothing but the background.

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {   
    CGFloat initialGraphContentsWidth = graph.frame.size.width - graph.leftMargin;
    CGFloat initialGraphContentsHeight = graph.frame.size.height - graph.bottomMargin - graphHelper.topMargin;

    self.frame = CGRectMake(0, 0, initialGraphContentsWidth, initialGraphContentsHeight);
    self.position = CGPointMake(graphHelper.leftMargin + 1, graphHelper.topMargin + 1);
    self.backgroundColor = [UIColor greenColor].CGColor;

    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextAddRect(context, CGRectMake(0, 0, 20, 20));
}

So how to I dynamically change my bounds and still get my content to appear?

I tried setting up a CATransaction with a duration of 0, but that did nothing.

DBD
  • 23,075
  • 12
  • 60
  • 84

1 Answers1

0

Have you tried calling setNeedsDisplay after you have updated the frame/bounds?

So for instance if you had a CALayer called layer, you could try the following:

layer.frame = CGRectMake(20, 40, 100, 300);
// tell the layer that it needs to refreshed
[layer setNeedsDisplay];
haroldcampbell
  • 1,512
  • 1
  • 14
  • 22
  • `setNeedsDisplay` causes in `drawLayer:inContext` being called. So calling `setNeedsDisplay` would at worst result in a infinite draw loop and best Apple's code would recognize the issue and just ignore the call. – DBD Apr 23 '12 at 12:14