我正在尝试在IKImageBrowserCell对象内的层上添加NSButton。我发现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;
}但不幸的是,这个按钮并没有出现在图层上。我认为按钮的位置很好,因为它是基于苹果应用程序的示例代码。我感觉这一行是错的:layer addSublayer :[uploadCheckBox layer]];,因为我应该添加整个NSButton,而不仅仅是它的位图表示(一个层)。非常感谢您的帮助!
发布于 2015-02-04 15:43:06
不能在CALayer中添加NSView。您应该为您的按钮用途创建一个新层,并添加到您的保持层。
https://stackoverflow.com/questions/28315776
复制相似问题