1

I have a UIImageView with an image already set, and try to add a CALayer as a subview. It works (sublayer count rises from 0 to 1) but nothing happens on the screen. Even if I try to set the layer background to black or set hidden to 'No'..

 CALayer*layer=[[CALayer alloc]initWithLayer:anotherUIImageView.layer];

[myUIImageVIew.layer addSublayer:layer];

[[myUIImageView.layer.sublayers objectAtIndex:0] setHidden:NO];
[[myUIImageView.layer.sublayers objectAtIndex:0] setBackgroundColor:[UIColor blackColor]];

Any idea?

1 Answer 1

5

Set some bounds to the layer and assign a colour, so you can see it

CALayer* layer = [CALayer layer];
layer.bounds = CGMakeRect(0, 0, 10, 20);
layer.backgroundColor = [[UIColor greenColor] CGColor];
[myUIImageVIew.layer addSublayer:layer];

Also, from the documentaion of [CALayer initWithLayer:]:

Note: Invoking this method in any other situation will produce undefined behavior. Do not use this method to initialize a new layer with an existing layer’s content.

1
  • Yes! Thanks Davyd, I must assume that for once I had not read the whole Apple documentation on this topic. Time was missing, so i lost it! thx again
    – NightCoder
    Oct 4, 2011 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.