iOS CALayer Creating a CALayer

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

You can create a CALayer and set its frame like this:

Swift:

let layer = CALayer()
layer.frame = CGRect(x: 0, y: 0, width: 60, height: 80)

Objective-C:

CALayer *layer = [[CALayer alloc] init];
layer.frame = CGRectMake(0, 0, 60, 80);

You can then add it as a sublayer to an existing CALayer:

Swift:

existingLayer.addSublayer(layer)

Objective-C:

[existingLayer addSublayer:layer];

Note:

To do this you need to include the QuartzCore framework.

Swift:

 @import QuartzCore

Objective-C

#import <QuartzCore/QuartzCore.h>


Got any iOS Question?