所以我基本上是在尝试制作一个可选择的文本项目列表(只是一个文本列表,没有按钮边框、背景等)。我想我可以使用 NSTableview 来实现这一点,但是试图使表格视图完全透明并且仍然可以正常工作给我带来了一些问题。Anwyays,我正在尝试使用我以编程方式创建并添加到列表中我的视图的 NSButtons 来完成它,没有任何背景或边框。但是,当我设置属性以使按钮透明且没有边框时,按钮的可点击区域将降级为单独的按钮标题文本。单击按钮应位于的其他任何位置(标题周围)不再起作用。这是我正在使用的代码。我希望能够单击矩形中创建按钮的任意位置以引起单击。仅供参考,我尝试过没有复选框图像的 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;
}
更新:我已经能够确认将背景颜色设置为清除似乎会导致按钮停止响应其完整边界内的点击(而不是移除边框)。