2
votes

Greetings all,

First of all thank you all for helping me out with my previous questions.

I see strange behavior when overriding drawRect: for NSView. I have a very simple NSView subclass (RoundedView) which only overrides drawRect. I have set the class of a custom view to RoudedView in interface builder. Inside that view I have a label. drawRect: is called correctly as expected but to my surprise, the RoundedView drawRect: is also called for the Label inside that view. This causes the rect for the label to be drawn by the drawRect that was only intended for the custom view and not for any other elements within that view. As a result the rect for the label is not correct. The custom view is inside a window that has the styleMask set to NSBorderlessWindowMask.

Is this correct behavior? How do I prevent drawRect being called for elements that are part of the view?

I have gone through all the related post here on the stack but unfortunately came up empty.

Thanks,

Robert

1

1 Answers

3
votes

Don't expect the rectangle passed to drawRect: to always be your view's bounds. NSView is capable of redrawing only the portions of itself that need redrawing (therefore, the parameter is called dirtyRect). In this case, the label overlaps the content of your view, so whenever it is redrawn, the view system also needs to redraw the view behind it (yours).

If your view's drawing isn't very complex, you can just ignore the rectangle that is passed to you and use the view's bounds instead, otherwise, you will need to figure out how to draw only the part of your view that is 'dirty'.