I want to create an arbitrary canvas in Cocoa; that is, a drawing area where the entire visible area can be drawn on and with scrolling limits chosen by the programmer and have no effect on whether I can draw on the entirety of the visible area. This is similar to how I would do this on Windows and GTK+, and different from the standard NSScrollView in that a) I draw solely in the visible area (so (0, 0) is the origin of the visible area, not the whole document) and b) I am not restricted to the document view frame (so if the document view is smaller than the visible area I can still draw everything in the visible area).
To do this, I'm implementing a subclass of NSClipView to do the drawing and using NSScrollView's document view solely to set scroll extents.
When I override drawRect:
in NSClipView, the background of the clip view turns black. The method can be empty, it can defer to super
, or it can draw anything; the result is the same. Only by omitting drawRect:
does the correct white background color draw.
Now, as hinted here (even though it's about iOS I assume the models are similar enough that the solution would have worked on OS X too), if I override isOpaque
to return NO
, the background draws correctly. Why? What is different about NSClipView that it can get away with returning YES
while still drawing the background?
Or is my approach to implementing this scroll view wrong?
Thanks.
NSClipView
, but that class is bound to have some special treatment in the framework. – Ken Thomases