0

I am currently trying to set a background color/pattern for an NSView that has a negative bounds origin. The problem is, that it only colors parts of the view (those where x>0 && y>0).

I understand, that I have to draw the background in the -(void)drawRect: function. How can I color the whole view, not just those parts where the coordinates are positive?

NSView Origin: -65, -65 Size: 130, 130

I want to color the whole background

Tim Bodeit
  • 9,673
  • 3
  • 27
  • 57
  • Which part of that is the view that's not drawing its whole background, and where is it in the view hierarchy? – Peter Hosey Mar 17 '13 at 22:12
  • I have an NSScrollView inside of which there is a Subclass of NSView. The Circle in the middle of the picture is an NSImageView, which is a subview of the NSView subclass. I can display subviews in the negative areas, but no background is drawn. What I did right now is that I created another NSView with the same Size and put it as a subview. While it is not what I imagined, it solves my specific problem. – Tim Bodeit Mar 18 '13 at 11:26

1 Answers1

0

Try using an NSBezierPath object to set the background colour of your view :

NSColor *color; //your background colour
CustomView *view; //this is your view, you should init it with the his own class
[color set];
[NSBezierPath fillRect:[view bounds]]; //sets the given Rect with the set colour

This code should colour all the background with the given colour. Note that you should replace the last line with [path fillRect:[self bounds]] if the code is in the implementation of the class of your view.

Moray
  • 321
  • 2
  • 9
  • Unfortunately this does not work. There is no instance method fillRect: for NSBezierPath. And if i use [NSBezierPath fillRect:[self bounds]]; I still have the same Problem. – Tim Bodeit Mar 17 '13 at 19:33
  • Yeah you're right it's a class method. Then try to create an `NSRect` with the properties of your view (same origin, width and height) and then call the `-fillRect` method with this rect. – Moray Mar 17 '13 at 19:48