Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give NSWindow a particular background color

I am writing a cocoa application which has a NSWindow. I want to change the background color of the window to a specific color. But the window properties in the inspector only provide "Textured Window" alternative. How can I make the color of the window as desired?

like image 975
shrads Avatar asked Dec 29 '08 11:12

shrads


People also ask

How do you give the background a color in Swift?

Find the view or view controller you want to change the background color of. Open it up in the interface builder and open the Attributes Inspector. Find the background color field and set it to the color you want.

How do you select a background Colour from the colors group?

Answer. Press "Windows," type "Paint" and click "Paint" to launch the Paintprogram. ... Click the image's background colorand note that Paint changes the color of the "Color 1" square to match that color. ... Move to the Colors section and click the color you'd like to use to replace the existing background color.


2 Answers

Try calling the instance method setBackgroundColor: with a color on your window instance. What's in a name.. ;)

Like this:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Option 1
    [self.window setBackgroundColor: NSColor.whiteColor];
    // Option 2 - using dot syntax
    self.window.backgroundColor = NSColor.whiteColor;
}
like image 75
Dirk Stoop Avatar answered Sep 22 '22 13:09

Dirk Stoop


The simplest way to change window background is to set it directly in your .xib file.

No code at all:

  1. Select your window (NSWindow class should appear in Class field)
  2. Click [+] button under the User Defined Runtime Attributes
  3. Type "backgroundColor" and select Color

Window Properties in XCode

like image 34
Oleg Korzhukov Avatar answered Sep 20 '22 13:09

Oleg Korzhukov