How do you override UIView's +(id)layer; class method?

眉间皱痕 提交于 2019-12-04 14:34:49

问题


I read in the documentation that a UIView's 'layer' property is read only and that you must override UIView's

+ (id)layer;

class method to access the layer's styling properties.

Are there any examples of overriding this method to return a layer/view with styling properties already applied?


回答1:


If you just want to set properties like backgroundColor, opacity, etc. on the default CALayer that's assigned to the UIView, you can set those on the UIView's layer at any time using something like the following:

view.layer.opacity = 0.0f;

The only time that you would need to override the - (CALayer)layer method would be if you wanted to return a custom CALayer subclass. I believe that on the iPhone Apple recommends you override the class method layerClass instead. This will return the CALayer subclass to be created when initializing your custom view. For example,

+ (Class) layerClass 
{
    return [CAEAGLLayer class];
}

causes your UIView subclass to use an OpenGL layer for its backing.



来源:https://stackoverflow.com/questions/724342/how-do-you-override-uiviews-idlayer-class-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!