NSButton的可选区域

时间:2011-02-12 07:50:23

标签: cocoa nsbutton

所以我基本上试图制作一个可选择的文本项列表(只是一个文本列表,没有按钮边框,背景等)。我想我可以用NSTableview来实现这一点,但是试图使表视图完全透明并且仍然可用,这给了我一些问题。 Anwyays,我正在尝试使用我以编程方式创建的NSButton并在列表中添加到我的视图中,没有任何背景或边框。但是,当我设置属性以使按钮透明且没有边框时,按钮的可单击区域将被降级为单独按钮标题的文本。单击按钮所在的任何其他位置(标题周围)不再有效。这是我正在使用的代码。我希望能够在我创建按钮的矩形中的任何位置单击以便进行单击。仅供参考我在没有复选框图像的情况下尝试过NSSwitchButton,这是一回事。谢谢你的帮助!

for(NSString *theTask in theTasks){
    NSButton *theCheckBox = [[[NSButton alloc] initWithFrame:NSMakeRect(xCoordinate + 25, yCoordinate + ([tasksWindow frame].size.height/2) - 60, [tasksWindow frame].size.width - 40, 25)] autorelease];
    [theCheckBox setButtonType:NSToggleButton];
    [theCheckBox setAction:@selector(taskChecked:)];
    [[theCheckBox cell] setBackgroundColor:[NSColor clearColor]];
    [[theCheckBox cell] setBordered:NO];
    NSAttributedString *theTitle = [[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", theTask] attributes:[NSDictionary dictionaryWithObject:[NSColor whiteColor] forKey:NSForegroundColorAttributeName]] autorelease];
    [theCheckBox setAttributedTitle:theTitle];
    [[tasksWindow contentView] addSubview:theCheckBox];
    yCoordinate -= 20;
}

更新:我已经确认将背景颜色设置为清除似乎会导致按钮停止响应其完整边界内的点击(而不是删除边框)。

1 个答案:

答案 0 :(得分:0)

所以回答我自己的问题,那是因为我在透明的NSWindow(拒绝鼠标事件)上覆盖了透明按钮。我只需要设置窗口不要忽略鼠标事件,行为就会消失。