title: Subclassing NSScrollView drawRect: Method
body: <p>I'm customizing the UI for one of my apps, and the idea is that a text area is initially bordered gray when out of focus, and when it comes into focus, the border becomes bright white. My app uses a dark theme, and for a single-lined <code>NSTextField</code>, this works great.</p>
  
  <p>I'm running into problems with a subclassed <code>NSTextView</code>, however. In order to alter the border properly, I ended up having to actually subclass the parent <code>NSScrollView</code>, but am still seeing strange behavior. <em>(See screenshot below.)</em> I <strong>want</strong> the red box to fill the entire scroll view, as this would allow me to stroke (instead of filling, which is just for testing) the path, producing a nice border. Instead, the red box seems to be only filling to the internal child view.</p>
  
  <p>The following code snippet, which is for the <code>NSScrollView</code> subclass:</p>
  
  <pre><code>- (void)drawRect:(NSRect)dirtyRect {
      [super drawRect:dirtyRect];
  
      NSRect borderRect = self.bounds;
      borderRect.origin.y += 1;
      borderRect.size.width -= 1;
      borderRect.size.height -= 4;
  
      BOOL inFocus = ([[self window] firstResponder] == self);
  
      if (!inFocus) {
          inFocus = [self anySubviewHasFocus:self];
      }
  
      if (inFocus) {
          [[NSColor colorWithDeviceRed:.8 green:.2 blue:0 alpha:1] set];
      } else {
          [[NSColor colorWithDeviceRed:.1 green:.8 blue:0 alpha:1] set];
      }
  
      [NSGraphicsContext saveGraphicsState];
      [[NSGraphicsContext currentContext] setShouldAntialias:NO];
      [NSBezierPath fillRect:borderRect];
      [NSGraphicsContext restoreGraphicsState];
  
      NSLog(@"My bounds: %@", NSStringFromRect(borderRect));
      NSLog(@"Super (%@) bounds: %@", [self superview], NSStringFromRect(borderRect));
  }
  </code></pre>
  
  <p>Produces the screenshot as seen below. Also, see the output in the log, which <em>suggests</em> that the entire view should be filled. This is the only output that is ever shown, regardless of the size of the text inside. Entering carriage returns increases the height of the <strong>red</strong> box, but does not produce different output. (And I would like the <strong>red</strong> box to fill the entire bounds.)</p>
  
  <pre><code>2011-04-08 21:30:29.789 MyApp[6515:903] My bounds: {{0, 1}, {196, 87}}
  2011-04-08 21:30:29.789 MyApp[6515:903] Super (&lt;EditTaskView: 0x3a0b150&gt;) bounds: {{0, 1}, {196, 87}}
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/JHoHv.png" alt="Drawing Issue"></p>
  
  <p><strong>Edit:</strong> Thanks to <strong>Josh Caswell</strong> for his answer. See below for the proper behavior when not focused, and when focused.</p>
  
  <p><img src="https://i.stack.imgur.com/W10CK.png" alt="No Focus"></p>
  
  <p><img src="https://i.stack.imgur.com/LETby.png" alt="Focus"></p>