2011-10-13 16 views
8

Quando si crea un pulsante di smussatura Cocoa con un'immagine personalizzata e un'immagine alternativa, sto avendo uno strano comportamento. Nello stato premuto lo sfondo del pulsante diventa bianco. Sto aggiungendo il pulsante come sottoview di una finestra trasparente (finestra HUD).NSButton sfondo bianco quando si fa clic su

sto cercando ogni tecnica che conosco:

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

Ho anche provato

[closeButton setButtonType:NSMomentaryChangeButton]; 

[[closeButton cell] setHighlightsBy:NSContentsCellMask]; 

senza risultati.

È possibile vedere il comportamento sbagliato negli screenshot allegate:

pulsante Bevel sovrapponendo una finestra HUD:
Bevel button overlaying a HUD window

sbagliato pulsante smussatura sfondo: pulsante
Wrong Bevel button background

risposta

2

Creazione

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

pulsante Aggiungi a finestra

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

Non mi è chiaro qual è questa NSWindow myWindow. È il contenitore del pulsante? In questo caso il suo contenuto rect è definito come NSRect rect? – loretoparisi

+0

è chiaro ora? –

+0

Sembra fantastico! – loretoparisi

25

A seconda della situazione, questo può anche funzionare:

cambiare lo stile del pulsante per Bevel o della Piazza, la modalità deve essere impostato su "Momentary Change" e di frontiera , Trasparente, Misto e Selezionato dovrebbe essere OFF. Ecco come ho risolto il problema dello sfondo bianco sui miei pulsanti.

+0

Questo è un problema su Mac OS 10.12 ma non su 10.13. – samatron

1

È necessario impostare il tipo di pulsante: myButton.buttonType = NSMomentaryChangeButton;

4

ho fatto il lavoro impostando cell.highlightsBy-ContentsCellMask:

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) 

In questo modo il pulsante viene oscurato quando viene premuto, ma non è un brutto piazza appare. Testato solo in El Capitan).

+0

puoi anche aggiungere 'btn.alternateImage = myButtonAlternateImage' e verrà mostrato quando il pulsante è premuto –

Problemi correlati