我正在尝试在 IKImageBrowserCell 对象内的图层上添加 NSButton。我发现这篇文章很有帮助,但它没有进入症结所在。我已经尝试过这个:
- (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;
}
但不幸的是,该按钮没有出现在图层上。我认为按钮的位置很好,因为它基于 Apple 应用程序的示例代码。我感觉这条线是错误的:
layer addSublayer :[uploadCheckBox layer]];
,因为我应该添加整个 NSButton,而不仅仅是它的位图表示(一层)。非常感谢任何帮助!