Set the border of a subclassed NSTableCellView

659 views Asked by At

As mentioned in the title of this post I want to set the border (color and width) of a subclassed NSTableCellView which is used inside a view-based NSTableView. I tried the following

self.layer.borderColor = [[NSColor greenColor] CGColor];
self.layer.borderWidth = 3.0;

I placed the code in initWithCoder and awakeFromNib without the desired result. Changing the backgroundColor is possible within the drawRect-Method. Can someone point me to the right direction?

Thanks

EDIT

Here is my solution using NSFrameRect

- (void)drawRect:(NSRect)dirtyRect
{
    [NSGraphicsContext saveGraphicsState];

    [[NSColor lightGrayColor]set];
    NSFrameRect([self bounds]);

    [NSGraphicsContext restoreGraphicsState];
}
1

There are 1 answers

2
uchuugaka On BEST ANSWER

Views on OS X are not layer backed by default. You first need to setWantsLayer: YES

But if you are using drawRect: you can just use NSFrameRect() or one of the similar functions or draw with an NSBezierPath in you cell view subclass. However, keep in mind that usually the row view does background drawing in view based tables.

Sounds like you have a bit of learning yet to do about drawing in Cocoa.