问题
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