The property layer of UIView is described in Apple's doc as following:
layer
The view’s Core Animation layer used for rendering. (read-only)
@property(nonatomic, readonly, retain) CALayer *layer
It is obviously that it is readonly. But in my project, why it could be set as following:
NSLog(@"before: %f",self.myView.laye.frame.size.width);
[self.myView.layer setAffineTransform:CGAffineTransformMakeScale(2, 2)];
NSLog(@"after: %f",self.myView.laye.frame.size.width);
//log shows us that the frame is modified
Really confused in this situation. Anyone can help me out? Thanks in advance!
The layer
property is read-only, it means you cannot change the layer for another, however the CALayer
object contained in the property is not immutable, you can set its own properties.
You cannot do:
self.myView.layer = newLayer;
// equivalent to [self.myView setLayer:newLayer];
But you can do:
[self.myView.layer setAffineTransform:CGAffineTransformMakeScale(2, 2)];
It's the setLayer:
selector that you can't use.
CALayer is not part of UIKit. It’s part of the Quartz Core framework
while UIView class is a part of UIKit. You can read the documentation of both to know the differences
UIView inherits from NSObject and CALayer also inherits from NSObject so at the time you are doing: [self.myView.layer setAffineTransform:CGAffineTransformMakeScale(2, 2)];
You are not assigning the layer , you are actually accessing the CALayer class layer properties directly and therefore you can play with position, size, and transformation of a layer, as you can see in CALayer documentation, it allows all these things
来源:https://stackoverflow.com/questions/12837077/isnt-property-layer-of-uiview-be-readonly