2011-10-13 24 views
8

Özel resim ve alternatif resim içeren Cocoa eğim düğmesi oluştururken garip bir davranış yaşıyorum. Basılmış durumda, düğme arka planı beyaz olur. Düğmeyi şeffaf bir pencerenin alt görünümü olarak ekliyorum (HUD penceresi). Bildiğim her tekniği çalışıyorumNSButton tıklandığında beyaz arka plan

:

NSButton *closeButton = [[NSButton alloc] initWithFrame:NSMakeRect(0.0, 0.0, 30.0, 30.0)]; 
     [closeButton setFrameOrigin:NSMakePoint(0.0, 0.0)]; 
     [closeButton setImagePosition:NSImageOnly]; 
     [closeButton setAction:@selector(closeWindowAction:)]; 
     [closeButton setBordered:NO]; 
     [closeButton setTransparent:NO]; 

     [closeButton setImage:[NSImage imageNamed:@"icon-tclose-off"]]; 
     [closeButton setAlternateImage:[NSImage imageNamed:@"icon-tclose-on"]]; 
     [closeButton setBezelStyle:NSShadowlessSquareBezelStyle]; 
     [closeButton setButtonType:NSMomentaryLightButton]; 

     //[[closeButton cell] setBackgroundColor:[NSColor clearColor]]; 
     [[closeButton cell] setHighlightsBy:NSChangeBackgroundCellMask|NSCellLightsByContents]; 
     //[[closeButton cell] setHighlightsBy:NSContentsCellMask]; 
     //[[closeButton cell] setShowsStateBy:0|NSContentsCellMask]; 

Ben de hiçbir sonuçlarla

[closeButton setButtonType:NSMomentaryChangeButton]; 

[[closeButton cell] setHighlightsBy:NSContentsCellMask]; 

çalıştı.

ekteki ekran görüntüsünde yanlış davranışı görebilirsiniz:

bir HUD penceresini kaplayan

Konik düğmesi:
Bevel button overlaying a HUD window

Yanlış konik düğmesi arka planı:
Wrong Bevel button background

cevap

2

oluşturma düğmesi

NSButton *myButton; 
myButton = [[NSButton new] autorelease]; 
[myButton setTitle: @"Hello!"]; 
[myButton sizeToFit]; 
[myButton setTarget: self]; 
[myButton setAction: @selector (function:)]; 

Durumunuza bağlı pencerenin

unsigned int styleMask = NSTitledWindowMask 
          | NSMiniaturizableWindowMask; 
NSWindow *myWindow; 
myWindow = [NSWindow alloc]; 
/*get the size of the button*/ 
NSSize buttonSize; 
buttonSize = [myButton frame].size; 
/*set window content rect with the size of the button, and with an origin of our choice; (100, 100)*/ 
NSRect rect; 
rect = NSMakeRect (100, 100, 
        buttonSize.width, 
        buttonSize.height); 

myWindow = [myWindow initWithContentRect: rect 
         styleMask: styleMask 
         backing: NSBackingStoreBuffered 
         defer: NO]; 
[myWindow setTitle: @"my window"]; 
/*replacing the default window content view with our button*/ 
[myWindow setContentView: myButton]; 
+0

Bu NSWindow myWindow olan benim için net değil. Bu düğme kabı mı? Bu durumda içeriği rect, NSRect rect olarak tanımladığınız şeydir? – loretoparisi

+0

şimdi açık mı? –

+0

Harika görünüyor! – loretoparisi

25

için düğmeye ekleyin, bu da işe yarayabilir:

Değişim Eğim veya Meydanı'na düğmeye tarzı, modu ayarlanması gerekir "Anlık Değişim" ve Sınır etmek , Şeffaf, Karışık ve Seçilmiş KAPALI olmalıdır. Düğmelerimdeki beyaz arka plan problemini çözdüm.

+0

Bu Mac OS 10.12'deki bir sorun ancak 10.13'te değil – samatron

1

Düğme türünü ayarlamanız gerekir: myButton.buttonType = NSMomentaryChangeButton;

4

Ben ContentsCellMask için cell.highlightsBy ayarlayarak çalışmak yaptık:

let btn = NSButton(frame: myFrame) 

btn.image = myButtonImage 
btn.image?.size = myFrame.size 
btn.imagePosition = .ImageOnly 

btn.bordered = false  
(btn.cell as? NSButtonCell)?.highlightsBy = .ContentsCellMask 

view.addSubview(btn) 

basıldığında buton karartılmış Bu şekilde, ancak hiçbir çirkin kare görünür. Sadece El Capitan'da test edilmiştir).

+0

'da 'btn.alternateImage = myButtonAlternateImage' öğesini ekleyebilir ve tuşa basıldığında gösterilecektir –

İlgili konular