Question

I've a following problem. There is a subclassed NSScrollView with a view based NSTableView in it. I've added a custom background to the scrollview in the -drawRect: method of subclass, and now I would like to add some "padding" around the inner tableview like this:

example http://img.skitch.com/20120117-ktd9g5wy8u9cm37jeebjjxx61u.png

How can I implement this?

Was it helpful?

Solution

Finally, I've solved the problem. I've created an NSView (let's call it documentContentView), added my NSTableView as a subview of this documentContentView, then I've added the documentContentView to the scrollview's documentView:

NSTableView *docView = (NSTableView *)self.scrollView.documentView;
id newClipView = [[CustomClipView alloc] initWithFrame:[self.scrollView.contentView frame]];
[self.scrollView setContentView:(NSClipView *)newClipView];
[newClipView setDrawsBackground:NO];   

NSView *documentContentView = [[NSView alloc] initWithFrame:docView.bounds];
docView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[documentContentView addSubview:docView];
[self.scrollView setDocumentView:documentContentView];
[self.scrollView setDrawsBackground:NO];

I've created my custom NSClipView called CustomClipView (based on this article http://www.cocoadev.com/index.pl?CenteringInsideNSScrollView) and this subclass sets the origin of the documentContentView when the window resized. I've subclassed my tableview as well, and in -reloadData method I can resize the documentContentView when the tableview change it's contents.

OTHER TIPS

The left and right padding can be done inside of the row/cell itself. For the top and bottom padding I suggest to add additional rows with no content and not selectable. This is not sexy, but worked for me.

If you're targeting Mac OS 10.10 or later, you could use

[scrollView setAutomaticallyAdjustsContentInsets:NO];
[scrollView setContentInsets:NSEdgeInsetsMake(top, right, bottom, left)];

First of all, don't add backgrounds in drawRect:. Add it in your initWithFrame: if you're subclassing, or change it from the invoker.

Adding the padding is easy: Change the frame of the NSTableView so that it is smaller, and has an origin that isn't at 0,0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top