Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTableView with rounded corners

I'm trying to customize the UI of my application and I want my NSTableView to have rounded corners. So I subclassed NSTableView and got this:

Imgur

However, when I populate the table and select a row, the selection is drawn over the border like this:

Imgur

I've tried adding a clip in the table view drawing code and it doesn't work. Any suggestions for how I can fix this?

Edit:

My drawing code in the NSTableView is the following:

- (void)drawRect:(NSRect)dirtyRect {
    [NSGraphicsContext saveGraphicsState];
    NSRect frame = NSMakeRect(0.0, 0.0, [self bounds].size.width, [self bounds].size.height-1.0);
    [[NSBezierPath bezierPathWithRoundedRect:frame xRadius:3.6 yRadius:3.6] addClip];
    [super drawRect:dirtyRect];
    [NSGraphicsContext restoreGraphicsState];
}

The actual rounded frame is drawn in the NSScrollView drawRect method. The interesting thing is that this does clip the selection of the very first and very last rows:

Imgur

But not when the table is scrolling:

Imgur

So the question remains: how can I clip all drawing inside the rounded frame of the NSScrollView?

like image 490
danjonweb Avatar asked Mar 28 '26 13:03

danjonweb


1 Answers

I found that you can call this on the container scroll view of the table view.

self.scrollView.wantsLayer = TRUE;
self.scrollView.layer.cornerRadius = 6;

That's all I needed and it works. No subclassing needed.

like image 88
gngrwzrd Avatar answered Mar 30 '26 04:03

gngrwzrd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!