Reputation:
I want to set the background of an NSBox
to be a gradient. In Interface Builder it is possible to set the background color of an NSBox
to selectedMenuColor
which is a gradient.
NSBox
only has a setFillColor
method so how is Interface Builder filling it with a gradient?
How do I programmatically fill an NSBox
without subclassing it? It would be trivial to subclass NSBox
but the workings of Interface Builder suggest there may be better solution.
Upvotes: 1
Views: 5435
Reputation: 11949
In xib
, select NSBox
, then goto 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]];
Upvotes: 0
Reputation: 5662
selectedMenuColor
is a "magic" color that is not displayed 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 use as the pattern will get tiled, so you will probably have to resize the image to the size of the box.
Upvotes: 2
Reputation: 12055
The selectedMenuColor color is actually a pre-rendered image of a gradient, and not a gradient drawn on the fly, so there is not any way to specify an arbitrary gradient as a background color. Like Ben said, subclassing is probably the way to go.
Upvotes: 0
Reputation: 85542
Probably the closest you could come would be to use an NSColor created with colorWithPatternImage:
, then create the gradient you want as an image and load that in. Ugly, but should work. I think subclassing is your best bet.
Upvotes: 0