你如何覆盖UIView的+(id)层;类方法?

时间:2023-01-13 09:07:52

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

我在文档中读到UIView的'layer'属性是只读的,你必须覆盖UIView的

+ (id)layer;

class method to access the layer's styling properties.

class方法来访问图层的样式属性。

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

是否有任何覆盖此方法的示例返回已应用样式属性的图层/视图?

1 个解决方案

#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:

如果您只想在分配给UIView的默认CALayer上设置backgroundColor,opacity等属性,可以使用以下内容随时在UIView的图层上设置这些属性:

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,

如果要返回自定义CALayer子类,则唯一需要覆盖 - (CALayer)图层方法的时间。我相信在iPhone上,Apple建议您改写类方法layerClass。这将返回初始化自定义视图时要创建的CALayer子类。例如,

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

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

导致您的UIView子类使用OpenGL层作为其支持。

#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:

如果您只想在分配给UIView的默认CALayer上设置backgroundColor,opacity等属性,可以使用以下内容随时在UIView的图层上设置这些属性:

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,

如果要返回自定义CALayer子类,则唯一需要覆盖 - (CALayer)图层方法的时间。我相信在iPhone上,Apple建议您改写类方法layerClass。这将返回初始化自定义视图时要创建的CALayer子类。例如,

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

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

导致您的UIView子类使用OpenGL层作为其支持。