注意:我不寻求解决实际问题。我出于好奇问。
我有一个自定义 UIView
我在应用程序的主视图中使用的子类。它有一个 Metal 层类:
+ (id)layerClass
{
return [CAMetalLayer class];
}
我注意到如果我覆盖 -(void)drawRect:(CGRect)rect
方法,错误消息开始出现:
<CAMetalLayer: 0x17005dc70>: calling -display has no effect.
更有趣的是,即使我这样覆盖它,它也会出现:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
}
据我所知,这根本不可能有任何区别,因为根据 OOP 的基本原则,这样的覆盖(仅调用基类)不应该修改程序的行为。
这个错误消息到底是什么意思?为什么会出现?
最佳答案
UIView
API 旧 而且不是一个很好的 API。 UIView
和CALayer
之间的交互比较复杂。有趣的是,系统会检查您是否正在覆盖 drawRect
。这就是 UIView
文档中出现此警告的原因:
drawRect: - Implement this method if your view draws custom content. If your view does not do any custom drawing, avoid overriding this method.
不需要调用[super drawRect:rect]
。父类(super class)不进行实际绘图。如果您不想绘制任何东西,请完全删除该方法。如果您想在图层上绘制自定义内容,请不要调用 super
(仅当您知道父类(super class)也已覆盖 drawRect
时才调用 super)。
If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.
在你的情况下,我相信调用 [super drawRect]
做了一些不应该发生的事情。
关于ios - 为什么重写 UIView.drawRect 会出现 "calling -display has no effect"消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29300154/