Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setOpaque:NO vs setBackgroundColor:[NSColor clearColor]

I'm going through a tutorial on drawing a custom [shaped] window with cocoa by subclassing NSWindow.

The tutorial states that in initializer developer should do the following:

[self setOpaque:NO];
[self setBackgroundColor:[NSColor clearColor]];

So i'm wondering what is the differnce between these two messages and why are they needed both since their result is the same.

References: tutorial can be found here.

like image 836
Eimantas Avatar asked Dec 14 '22 02:12

Eimantas


2 Answers

I guess that the first message is required because the drawing system needs to know whether it should bother updating views that lie behind yours. For example, if a window in another application (behind your window) updates (say text appears etc) the windowing system would not normally need to redraw it, but since your window is transparent it does in this case.

like image 172
Ira Cooke Avatar answered Dec 27 '22 14:12

Ira Cooke


So i'm wondering what is the differnce between these two messages and why are they needed both since their result is the same.

They're not the same.

Look at the documentation for the opaque property: It's how you tell NSView that you're going to draw in your entire bounds, completely covering anything below your view.

If you don't cover the entire bounds, or you don't always draw at 100% opacity, then your view is not opaque, and you should leave that property set to NO.

If you set your view's background color to clearColor (which is simply a color with 0% opacity), and don't draw at 100% opacity over the entire background, then your view is not opaque.

On the other hand, it is possible to have clearColor as your background and then completely draw over it, in which case your view is opaque and should set itself as such.

like image 23
Peter Hosey Avatar answered Dec 27 '22 16:12

Peter Hosey