0

I have a Table subclass of NSView which contains several blocks of the Block subclass of NSView. This is the code for Table's drawRect: method:

- (void)drawRect:(NSRect)dirtyRect
{
    [NSGraphicsContext saveGraphicsState];
    [[[NSColor whiteColor] colorWithAlphaComponent:0.6] set];
    [NSBezierPath fillRect:dirtyRect];
    [NSGraphicsContext restoreGraphicsState];
}

And this the code of Block's drawRect: method:

-(void)drawRect:(NSRect)dirtyRect
{
    [NSGraphicsContext saveGraphicsState];
    [color set];
    [NSBezierPath fillRect:dirtyRect];
    [NSGraphicsContext restoreGraphicsState];
}

where "color" is set to [[NSColor whiteColor] colorWithAlphaComponent:0].

But I don't understand why, when I run the application, evenrything's fine, but each time I click on a Block this gets filled with the classic gray initial background on Mac OS X... can anybody help? I would be very greatful, thanks.

BigCola
  • 312
  • 1
  • 4
  • 16

1 Answers1

0

Change the Block's drawRect call to "colorWithAlphaComponent" to "1.0" and I think you'll be all set.

An alpha of zero means a transparent opacity. Check out Apple's "Color Programming Topics" document and you'll see them mentioning "alpha" a few times.

p.s. you may want to change the class name of "Block", since it might confuse people who are thinking of traditional Objective C blocks ^ { }.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Yes but I wanted it to be transparent because I originally wanted to add a gradient in Table's background... when I tried to do this, though, every time I'd click on a Block it would add the gradient to its background, and I don't understand why.. Hope you can help – BigCola Nov 03 '12 at 20:31
  • @MichaelDautermann Michael, could you help me with my drawRect problem: http://stackoverflow.com/questions/39855349/how-to-make-2-contradictory-methods-work-in-drawrect – Andy Jazz Oct 07 '16 at 08:29