JWWalker
JWWalker

Reputation: 22707

Why is my CALayer being clipped by my NSView?

The Core Animation Programming Guide led me to believe that a sublayer could, by default, extend outside the bounds of a host view without being clipped. But that's not what's happening for me.

Here's how I initialize my layer-hosting custom view:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self != nil) {
        mBaseImage = [[NSImage imageNamed: @"Button.png"] retain];
        mSliderImage = [[NSImage imageNamed: @"Track.png"] retain];

        mRootLayer = [CALayer layer];
        mRootLayer.masksToBounds = NO;
        [self setLayer: mRootLayer ];
        [self setWantsLayer: YES];
        mRootLayer.contents = mBaseImage;

        mSliderLayer = [CALayer layer];
        [mRootLayer addSublayer: mSliderLayer];
        NSSize imSize = [mSliderImage size];
        NSRect sliderBounds = NSMakeRect( 0.0f, 0.0f, imSize.width, imSize.height );
        mSliderLayer.bounds = sliderBounds;
        mSliderLayer.contents = mSliderImage;
        mSliderLayer.position = NSMakePoint( 31.0f, 31.0f );
    }

    return self;
}

The host view, and the image used as its contents, are 62x62, while the sublayer image is 90x10. But the whole thing gets clipped to the 62x62 bounds. What am I missing?

Upvotes: 0

Views: 622

Answers (1)

JWWalker
JWWalker

Reputation: 22707

The trick seems to be that the view must be contained in another view (maybe the content view of the window) that has a layer. (Is that documented?)

Upvotes: 5

Related Questions