Reputation: 16941
I am trying to add a CALayer as a sublayer of another CALayer. However only the parent layer gets displayed. Here's my code:
//display a green square:
CALayer *shipContainer = [CALayer layer];
shipContainer.bounds = CGRectMake(0,0,200,200);
shipContainer.position = CGPointMake(600,500);
shipContainer.borderColor = [UIColor greenColor].CGColor;
shipContainer.borderWidth = 3;
//display a red dot inside the square:
CALayer *ship1 = [CALayer layer];
ship1.bounds = CGRectMake(0,0,20,20);
ship1.position = CGPointMake(600,500);
ship1.cornerRadius = 10;
ship1.backgroundColor = [UIColor redColor].CGColor;
[shipContainer addSublayer:ship1];
I then call [self.view.layer addSublayer:shipContainer];
but only the green square is displayed. Any thoughts?
Upvotes: 1
Views: 1164
Reputation: 8564
As per documentation :
Position
The position property is a CGPoint that specifies the position of the layer relative to its superlayer, and is expressed in the superlayer's coordinate system.
so you need to change
ship1.position = CGPointMake(600,500);
so that ship1
can come in visible area. As superlayer has 200,200
as its bounds you need to make position's x
and y
less then these values.
Upvotes: 1