How to tell what's causing drawRect to be called?

萝らか妹 提交于 2019-12-11 10:33:26

问题


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.

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?

Thanks!


回答1:


First of all you shouldn't be adding subviews in 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:.

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.

EDIT: Just realized that you were asking about NSView and not UIView. I've never used NSView, but perhaps they work similarly.

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



来源:https://stackoverflow.com/questions/5375128/how-to-tell-whats-causing-drawrect-to-be-called

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