Search code examples
ioscalayernsbutton

changing NSButton CALayer hides its title


I'm trying to change a button background by this way :

    [table[i][j] setAttributedTitle:attrString];
    [table[i][j] setTitle:@"-"];
    table[i][j].tag=i*10+j;
    table[i][j].frame = CGRectMake((i*30+40), (j*30+106), 30.0, 30.0);
    [self.mainView addSubview:table[i][j]];
    CALayer *buttonLayer = [CALayer layer];
    buttonLayer.contents=(id)[NSImage imageNamed:@"icaseinactiv"];
    [table[i][j] setWantsLayer:YES]; // view's backing store is using a Core Animation Layer
    [table[i][j] setLayer:buttonLayer];

However, once I apply the new layer, its title disappears (the title is still there when I remove the last line. What am I doing wrong ?

Thanks in advance


Solution

  • Well I'm pretty sure NSButton won't be using a layer by default, so it will be drawing using the normal [NSView drawRect:] method. You then added a layer which covers this canvas and voilà, this is what happens.

    In order to do what you want you will need to subclass NSButtonCell I reckon.