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:

3 Answers
3

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:

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.

Most probably I misunderstood the answer. I've overwrite the -frame method of my subclassed tableview: - (NSRect)frame { NSRect aFrame = [super frame]; NSRect newFrame = NSMakeRect(0, 0, aFrame.size.width + 20, aFrame.size.height + 20); newFrame.origin.x = -10; newFrame.origin.y = -10; return newFrame; } It almost works, but unfortunately the right side of the table isn't visible in the scrollview, and it seems that the scrollview couldn't handle my changes properly because if the bottom half row is out of the visible area the right scroller doesn't appear automaticaly.
–
vbaliJan 17 '12 at 15:36

I don't have the time to explain this in detail to you right now, but please look up UIScrollView.contentSize to tell the scrollView how big its content is supposed to be. Also, in your newFrame, the sizes should be -20, not +20. Please also note that ordinarily, a NSTableView scrolls by itself if needed (you can see in the docs that it is a subclass of UIScrollView); there is no need to wrap it in a scrollView if you don't want anything extraordinary.
–
fzwoJan 17 '12 at 15:40

Unfortunately NSScrollView's contentSize is a read-only property. I've overwritten the -contentSize accessor of my NSScrollView's subclass and return the frame.size of it's documentView, but it doesn't work :( I've read the whole documentation of NSScrollView, NSTableView and NSClipView but I still don't understand how could I done with this simple task.
–
vbaliJan 17 '12 at 16:05

I'm sorry, I shouldn't have mixed these two issues in one comment (I also mixed up Mac and iOS). You can probably get the padding you want by setting the frame of the tableView to something like NSMakeRect(-10, -10, [super frame].size.width -20, [super frame].size.height -20).
–
fzwoJan 17 '12 at 16:43

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.