How do I add a NSButton to a CALayer?

439 views Asked by At

I am trying to add a NSButton on a layer inside a IKImageBrowserCell object. I found this post helpful but it doesn't get into the crux. I've already tried this:

- (CALayer *) layerForType:(NSString*) type
{
    CGColorRef color;

    //retrieve some usefull rects
    NSRect frame = [self frame];
    NSRect imageFrame = [self imageFrame];
    NSRect relativeImageFrame = NSMakeRect(imageFrame.origin.x - frame.origin.x, imageFrame.origin.y - frame.origin.y, imageFrame.size.width, imageFrame.size.height);

    /* foreground layer */
    if(type == IKImageBrowserCellForegroundLayer){
        //no foreground layer on place holders
        if([self cellState] != IKImageStateReady)
            return nil;

        //create a foreground layer that will contain several childs layer
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);


        //add a checkbox to tell whether to upload this one or not
        NSRect checkFrame  = NSMakeRect( ( frame.size.width/2)-5 , frame.size.height - 19, 18,18);
        NSButton *uploadCheckBox = [[NSButton alloc] initWithFrame:checkFrame];
        [uploadCheckBox setButtonType:NSSwitchButton];
        [layer addSublayer :[uploadCheckBox layer]];

        return layer;
    }
    //(...)
    return nil;
}

But unfortunately the button doesn't appear on the layer. I think the placement of the button is fine, since it's based on an example code from Apple's app. I've got a feeling that this line is wrong: layer addSublayer :[uploadCheckBox layer]];, since I should be adding entire NSButton, not just it's bitmap representation (a layer). Any help greatly appreciated!

1

There are 1 answers

0
Sheen Vempeny On

You cannot add a NSView inside a CALayer. You should create a new layer for your button purpose and add to your holding layer.