Setting NSBox background to gradient programmatically without subclassing
I want the background to NSBox
be a gradient. In Interface Builder, you can set the background color NSBox
- selectedMenuColor
which is a gradient.
NSBox
only has a method setFillColor
, so how does the Interface Builder fill it with a gradient?
How do I programmatically populate NSBox
without subclassing? It NSBox
would be trivial for a subclass , but the work of the Interface Builder suggests that there might be a better solution.
selectedMenuColor
is a "magic" color that does not appear as a solid color. Many of these "magic" colors exist in the system.
I have used colorWithPatternImage:
for this before. But note that the image you are using as a template will be tiled, so you may have to resize the image to fit the window.
source to share
In xib
select NSBox
then go to effect inspector, check NSBox
for Core Animation Layer. Now
IBOutlet NSBox *box;
[box.setWantsLayer:YES];
[box.layer setBackgroundColor:[[NSColor whiteColor] CGColor]];
or
[box.setWantsLayer:YES];
[box.layer setBackgroundColor:[[NSColor colorWithPatternImage:[NSImage imageNamed:@"white.gif"]] CGColor]];
source to share