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.

+1


source to share


4 answers


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.

+2


source


Perhaps the closest you could be to the NSColor created with colorWithPatternImage:

and then create the gradient you want as an image and load it. Awful, but it should work. I think subclassing is the best choice.



0


source


The selected MenuColor is actually a pre-rendered gradient image, not an on-the-fly gradient, so there is no way to specify an arbitrary gradient as the background. As Ben said, subclassing is probably the way to go.

0


source


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]];

      

0


source







All Articles