How can I subclass the CALayer to revert to pre-styled?
How do you subclass the CALayer so that you return a layer with style properties already set?
So to:
MyCustomLayer *layer = [MyCustomLayer layer];
will create a layer with these properties;
layer.backgroundColor = sweetBackgroundColor.CGColor;
layer.borderColor = sweetBorderColor.CGColor;
layer.borderWidth = 2.0;
layer.cornerRadius = 8.0;
So, I can just set its frame and add it to the tree.
But I think my question might really be just how do you subclass an object so that it has properties already set when created. Is it possible?
+1
source to share
1 answer
Just override -init (designated initializer for CALayer) in your subclass and set those properties in your override. Any create method, including -layer, must end up routing through the designated initializer, which means additional customization will happen even for convenience constructors.
+2
source to share