2014-02-08 66 views
2

我想用自己的圖標來實現一個無線電組。我發現的文件從apple如何實現圖標收音機組?

圖標單選按鈕

你也可以有一個單選按鈕,這是一個圖標按鈕;也就是說,它主要是通過它的圖標來標識的,並且很少或沒有文本。如果按鈕處於關閉狀態,則表明該按鈕處於插入狀態。如果該按鈕處於打開狀態,則表明該按鈕處於按下狀態(圖標按鈕無法顯示混合狀態)。

您可以在任一圖標中創建一組圖標單選按鈕界面生成器或以編程方式。如果您使用Interface Builder,請使用按鈕矩陣開始。如果以編程方式創建它,請創建一個按鈕矩陣。然後將矩陣的跟蹤模式更改爲NSRadioModeMatrix。將按鈕的類型更改爲NSPushOnPushOffButton,將它們的圖像位置更改爲NSImageOnly,將它們的擋板類型更改爲方形擋板類型。最後將他們的圖像設置爲你想要的。

所以,我寫了下面的代碼:

// self.matrix is bound in IB whose tracking mode is NSRadioModeMatrix 
NSArray *cellArray = [self.matrix cells]; 
// Set the button type to NSPushOnPushOffButton 
[[cellArray objectAtIndex:0] setButtonType:NSPushOnPushOffButton]; 
// Set image position to NSImageOnly 
[[cellArray objectAtIndex:0] setImagePosition:NSImageOnly]; 
// Set bezel type to square bezel type 
[[cellArray objectAtIndex:0] setBezelStyle:NSThickSquareBezelStyle]; 
// Finally set the image 
[[cellArray objectAtIndex:0] setImage:[NSImage imageNamed:@"ImageA"]]; 
// Do it all over again 
[[cellArray objectAtIndex:1] setButtonType:NSPushOnPushOffButton]; 
[[cellArray objectAtIndex:1] setImagePosition:NSImageOnly]; 
[[cellArray objectAtIndex:1] setBezelStyle:NSThickSquareBezelStyle]; 
[[cellArray objectAtIndex:1] setImage:[NSImage imageNamed:@"DensityMax"]]; 

但我得到的是不令人滿意,按鈕沒有邊框的邊框,以及選擇state沒有,我的意思是,我目前無法分辨哪一個被推入。

enter image description here

那我做錯了嗎?請爲我指出,非常感謝!

+0

更新了我的答案,我認爲你錯過了 – markhunte

回答

1

UPDATE *

如果我不這樣做完全編程,做它,你有。

我意識到,你還需要設置扣式電池的視覺代碼中

[[cellArray objectAtIndex:0] setBordered:YES]; 

創建矩陣完全編程接壤似乎並不需要邊界的設置:

NSRect windowContentViewRect = NSMakeRect(_cView.frame.origin.x +10, _cView.frame.origin.y+150, _cView.frame.size.width, _cView.frame.size.height); 
NSMatrix* matrix = [[NSMatrix alloc] initWithFrame:windowContentViewRect mode:NSTrackModeMatrix cellClass:[NSButtonCell class] numberOfRows:2 numberOfColumns:1]; 

[matrix setCellSize:NSMakeSize(100, 100)]; 
[matrix sizeToCells]; 
[matrix setNeedsDisplay:YES]; 

[_cView addSubview: matrix ]; 

[_cView setNeedsDisplay:YES]; 

NSArray *cellArray = [matrix cells]; 
// Set the button type to NSPushOnPushOffButton 
[[cellArray objectAtIndex:0] setButtonType:NSPushOnPushOffButton]; 
// Set bezel type to square bezel type 
[[cellArray objectAtIndex:0] setBezelStyle:NSThickSquareBezelStyle]; 
// Set image position to NSImageOnly 
[[cellArray objectAtIndex:0] setImagePosition:NSImageOnly]; 

// Finally set the image 
[[cellArray objectAtIndex:0] setImage:[NSImage imageNamed:@"ImageA"]]; 


// Do it all over again 
[[cellArray objectAtIndex:1] setButtonType:NSPushOnPushOffButton]; 
[[cellArray objectAtIndex:1] setBezelStyle:NSThickSquareBezelStyle]; 
[[cellArray objectAtIndex:1] setImagePosition:NSImageOnly]; 

[[cellArray objectAtIndex:1] setImage:[NSImage imageNamed:@"DensityMax"]]; 

enter image description here

+0

非常感謝,它幫助了我!爲什麼蘋果沒有提到.. –