Question

I have a custom view subclass similar to NSBox that draws a rounded box background. The problem is that if I place a view like an NSTableView in the box view, it does not clip to the rounded corners. Is there any way to round the corners of NSTableView and its parent scroll view?

Was it helpful?

Solution

I haven't tried this with a table view but have with other controls.

In a subclass of NSTableView (or whatever view/control you want to clip)

  1. Override drawRect:
  2. Create an NSBezierPath with the shape you want (probably appendBezierPathWithRoundedRect:xRadius:yRadius: just remember to use the view's bounds as the size)
  3. Send the path the addClip message to add that shape to the view's clipping path
  4. Call super's drawRect:

If the table view has a header you may need to clip the top corners by subclassing NSTableHeaderView. And if you have scrollbars you may have to do the same thing to them except only clip certain corners. Hopefully you don't have scrollbars because I doubt that would look right. Basically you want to clip the view/control that draws that part, clipping the parent will not cause subviews to be clipped.

If you look at Apple's Welcome to Xcode window they get away with it by drawing a custom header at the top and a text block at the bottom so they don't have to round the table view itself. If you can do something like that I would.

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