25

Is it possible to make an NSView not clip its subviews that are outside of the bounds? On iOS I would simply set clipsToBounds of my UIView no NO. But NSView doesn't have such a property. I tried experimenting with wantsLayer, masksToBounds, wantsDefaultClipping, but all of these seem to only change the clipping of the drawRect method, not the subviews.

DrummerB
  • 39,814
  • 12
  • 105
  • 142

6 Answers6

19

The behaviour around this seems to have changed. You just need to set the view's layer to not mask to bounds.

view.wantsLayer = true
view.layer?.masksToBounds = false
Avario
  • 4,655
  • 3
  • 26
  • 19
15

After 5 hours of struggling I've just achieve it. Just change class of any NSView in .storyboard or .xib to NoClippingView and it will NOT clip any of it's subviews.

class NoClippingLayer: CALayer {
    override var masksToBounds: Bool {
        set {

        }
        get {
            return false
        }
    }
}
    
class NoClippingView: NSView {
    override var wantsDefaultClipping: Bool {
        return false
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        wantsLayer = true
        layer = NoClippingLayer()
    }
}

Why do I override masksToBounds in NoClippingLayer? Because some native AppKit classes change this property of all sublayers in runtime without any warnings. For example, NSCollectionView do this for views of it's cells.

Nick
  • 3,958
  • 4
  • 32
  • 47
Padavan
  • 1,056
  • 1
  • 11
  • 32
  • And if you're not using .xib or want to avoid the step, simply add `open override func makeBackingLayer() -> CALayer { return NoClippingLayer() }` to `NoClippingView` – strangetimes Mar 24 '21 at 10:46
7

I was able to solve this by overriding wantsDefaultClipping of the subviews to return NO.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
  • 3
    Tested this with an `NSTextField` inside of a `NSButton` and it didn’t worked. – ixany Dec 21 '16 at 11:17
  • 1
    This was the accepted answer in 2013 but `wantsDefaultClipping` no longer work as it should. The later answer below (2018) with CALayers should now be the accepted way of doing this. – AndyTang Oct 31 '19 at 23:53
5

If you are using autolayout, override alignmentRectInsets to expand the clipping area, making it larger than the alignment rectangle. This example gives a space of 50 on all sides:

override var alignmentRectInsets: NSEdgeInsets {
    return NSEdgeInsets(top: 50.0, left: 50.0, bottom: 50.0, right: 50.0)
}
Valentin Shergin
  • 7,166
  • 2
  • 50
  • 53
Chase Finch
  • 5,161
  • 1
  • 21
  • 20
  • 1
    This expands the clipping area by increasing the view's frame. If you want the frame to remain the same, but increase the clipping area (as I think we're trying to do here), you'll have adjust all of your constraints to compensate. – jeremywhuff Aug 02 '17 at 21:26
0

wantsDefaultClipping method works... both the parent & the child need to override wantsDefaultClipping.

Once you go that route, however, cleaning up after yourself is cumbersome.

Here's my solution for attaching text to an NSProgressBar:

Thanks to BGHUDAppKit and stackoverflow

@implementation BGHUDOverlayTextField
- (BOOL) wantsDefaultClipping { return NO; } // thanks stackoverflow.com
@end


@interface BGHUDProgressIndicator : NSProgressIndicator {

    NSBezierPath *progressPath;
    NSString *themeKey;
   NSString *LayerKey; 
   BGHUDOverlayTextField *customTitleField;
   BOOL hiding;
}

@implementation BGHUDProgressIndicator
- (BOOL) wantsDefaultClipping { return (customTitleField == nil); } // thanks stackoverflow.com
- (void) setCustomTitle: (NSMutableAttributedString *)iTitle
{
   if ( !customTitleField )
   {
      NSRect r = [self bounds];
      r.size.height += 10;
      customTitleField = [[BGHUDOverlayTextField alloc] initWithFrame:r];
      [self addSubview: customTitleField];
   }

   [customTitleField setAttributedStringValue:iTitle];
}

- (void)setHidden:(BOOL)flag
{
   if ( customTitleField && flag && !hiding )
   {
      hiding = YES;
      NSRect fr = [self bounds];
      NSRect eraseFr = fr;
      eraseFr.size = [customTitleField frame].size;
      [self displayRectIgnoringOpacity:fr];
   }
   else if ( !flag )
      hiding = NO;
   [super setHidden:flag];
}

- (void) drawRect: (NSRect)fr
{
   if ( customTitleField )
   {
      fr = [self bounds];
      NSRect eraseFr = fr;
      eraseFr.size = [customTitleField frame].size;
      [[NSColor normalSolidFill] set];
      NSRectFill( eraseFr );
      if ( hiding )
      {
         [customTitleField setAttributedStringValue:nil];
         NSRectFill( eraseFr );
         return;
      }
      [NSGraphicsContext saveGraphicsState];
      NSRectClip(fr);
   }
   [super drawRect:fr];

   if ( customTitleField )
   {
      [NSGraphicsContext restoreGraphicsState];
   }
}
Keith Knauber
  • 752
  • 6
  • 13
0

I couldn't get any of these solutions to work. I think you may have to just change your view hierarchy so that views don't draw outside of their frame. You can create an intermediary view that doesn't do any drawing but has a larger frame to allow for a larger area.

jeremywhuff
  • 2,911
  • 3
  • 29
  • 33