According to the Apple UIView Programming Guide, you can only change a uiview’s CA layer using the layerClass method, which you must override to return a different class during the view’s initialization, and that this is the only way this can be done:
The type of layer associated with a view cannot be changed after the view is created. Therefore, each view uses the layerClass class method to specify the class of its layer object.
However, I’ve seen app tutorials that directly assign the view’s layer to another created layer, and I always assumed this was essentially replacing the layer with another layer class and worked fine.
Are these two methods going to yield the same results? Is it bad to use the second method?
The
layerproperty ofUIViewis read-only. You cannot assign to it. I suspect you were looking at a Mac OS X tutorial, not an iOS tutorial. On Mac OS X, you useNSView, which has a read-writelayerproperty.