VBK
VBK

Reputation: 1555

subview of layer backed nsview clipped

I have a subview of an layer backed nsview (set through storyboard) which exceed the bounds. For some reason it is getting clipped. Any idea why this is happening ?

Upvotes: 2

Views: 619

Answers (2)

Kakashi
Kakashi

Reputation: 31

I just solved the problem. It wasted me some time although the method is very simple.

There are two things need to pay attention to.

First of all (THE MOST IMPORTANT!!),you should set your winow's contentview.wantsLayer YES;

And then , set the parent view's layer.maskesToBounds NO;

What I did:

self.window.contentView.wantsLayer = YES;
 self.testview.layer.backgroundColor = [NSColor clearColor].CGColor;
 self.testview.layer.borderWidth = 3;
 self.testview.layer.borderColor = [NSColor blueColor].CGColor;
 self.testview.layer.masksToBounds = NO;
 NSView *aView = [[NSView alloc]initWithFrame:NSMakeRect(-50, -20, 100, 100)];
 aView.wantsLayer = YES;
 aView.layer.backgroundColor = [NSColor redColor].CGColor;
 [self.testview addSubview:aView];

The effect

enter image description here

GOOD LUCK

Upvotes: 1

Robert
Robert

Reputation: 1956

By default NSView will clip its subviews to its bounds.

If you have a layer backed NSView, alignmentRectInsets: might provide your solution. It returns an NSEdgeInsets that adds a margin to the layer's clipping bounds. Override the read-only property in your subview and return the desired insets.

If you need a more sophisticated way to inset the clipping, take a look at alignmentRectForFrame: and frameForAlignmentRect:.

See: developer.apple.com/…/Cocoa/Reference/ApplicationKit/Classes/NSView_Class

Upvotes: 1

Related Questions