title: Setting the background color of an NSView
body: <p>I want to set a custom view (not the main one) with a custom NSColor background (<code>[NSColor colorWithPatternImage:[NSImage imageNamed:@"pattern.png"]]</code>). I've tried making a custom view class:</p>
  
  <p>.h</p>
  
  <pre><code>#import &lt;AppKit/AppKit.h&gt;
  
  @interface CustomBackground : NSView {
      NSColor *background; 
  }
  @property(retain) NSColor *background;
  @end
  </code></pre>
  
  <p>.m</p>
  
  <pre><code>#import "CustomBackground.h"
  
  @implementation CustomBackground
  @synthesize background;
  
  - (void)drawRect:(NSRect)rect
  {
      [background set];
      NSRectFill([self bounds]);
  }
  
  - (void)changeColor:(NSColor*) aColor
  {
      background = aColor;
      [aColor retain];
  }
  @end
  </code></pre>
  
  <p>And then in the AppDelegate:</p>
  
  <pre><code>[self.homeView changeColor:[NSColor colorWithPatternImage:[NSImage imageNamed:@"pattern.png"]]];
  </code></pre>
  
  <p>But nothing happens, the color remains the same. What's wrong? Or is there an easier way? NSView doesn't have a <code>backgroundColor</code> property :(</p>