问题描述
我有一些自定义NSView,其中包含一些自定义按钮,这些按钮被添加为NSView的drawRect方法中的子视图。
I've got my custom NSView with a bunch of custom buttons in it, the buttons are added as a subView in the NSView's drawRect method.
现在我发现按下按钮后,父视图的drawRect被重复调用。有时候,当我退出应用程序时,它只会停止 - 我从drawRect中的一个简单的日志语句中知道这一点。
Now I'm finding that after pressing a button the drawRect of the parent view is repeatedly called. Sometimes it only stops when I quit the app - I know this from a simple log statement in drawRect.
现在我知道我的应用程序中可能有更大的体系结构问题导致,我去哪里开始跟踪导致这个视图被重复重绘的原因?
Now I know there are probably bigger architectural issues in my app causing this, where do I go to begin tracking down what's causing this view to be repeatedly redrawn?
谢谢!
推荐答案
首先,您不应该在 drawRect中添加子视图:
。
您是在做任何自定义绘图还是只是添加子视图?如果你没有做任何绘图,你不应该实现 drawRect:
。
Are you doing any custom drawing or are you just adding subviews? If you're not doing any drawing, you should not implement drawRect:
.
你想添加子视图在 initWithFrame:
中,然后您要根据在
layoutSubviews
中设置子视图的框架
You want to add the subviews in initWithFrame:
and then you want to set the frames of the subviews in layoutSubviews
based on self.bounds
.
如果您有任何问题,请随时问。
If you have any questions, feel free to ask.
编辑:只是意识到你在问NSView而不是UIView。我从来没有使用NSView,但也许他们的工作类似。
Just realized that you were asking about NSView and not UIView. I've never used NSView, but perhaps they work similarly.
编辑2:我读了一些关于NSView,它似乎不要有 layoutSubviews
。也许您应该将框架设置为 drawRect:
?我还是很确定你不想在 drawRect中添加子视图:
。
EDIT 2: I read a bit about NSView and it doesn't seem to have layoutSubviews
. Perhaps you should set the frames in drawRect:
? I'm still pretty sure you don't want to add subviews in drawRect:
.
这篇关于如何判断引起drawRect的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!