2011-02-12 77 views
0

所以我基本上試圖列出可選文本項目(只是文本列表,沒有按鈕邊框,背景等)。我想我可以用NSTableview做到這一點,但是試圖讓表格視圖完全透明並且仍然有用,這就給了我一些問題。 Anwyays,我試圖用編程方式創建NSButton,並將其添加到列表中的視圖中,而沒有任何背景或邊框。但是,當我將屬性設置爲使按鈕透明且沒有邊框時,該按鈕的可點擊區域將降級爲單獨按鈕標題的文本。點擊該按鈕應該在其他地方(標題周圍)不再有效。這是我正在使用的代碼。我希望能夠點擊我創建按鈕的矩形中的任何位置以引起點擊。僅供參考我已經嘗試過沒有複選框圖像的NSSwitchButton,它是同樣的事情。謝謝你的幫助!NSButton的可選區域

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; 
} 

更新:我已經能夠確認,設置背景顏色明確的是什麼似乎導致按鈕停止其全境內響應點擊(未去除邊框)。

回答

0

所以要回答我自己的問題,那是因爲我將透明按鈕疊加在透明的NSWindow上(它拒絕鼠標事件)。我只是不得不設置窗口忽略鼠標事件,行爲就消失了。