NightCoder
NightCoder

Reputation: 1139

CALayer not showing in UIImageView

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?

Upvotes: 1

Views: 2742

Answers (1)

Davyd Geyl
Davyd Geyl

Reputation: 4623

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.

Upvotes: 5

Related Questions