2011-10-13 28 views
8

Al crear el botón de bisel de Cocoa con imagen personalizada e imagen alternativa, estoy teniendo un comportamiento extraño. En estado comprimido, el fondo del botón se vuelve blanco. Estoy agregando el botón como subvista de una ventana transparente (ventana de HUD).NSButton fondo blanco al hacer clic

Estoy tratando todas las técnicas que yo sepa:

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

también probé

[closeButton setButtonType:NSMomentaryChangeButton]; 

[[closeButton cell] setHighlightsBy:NSContentsCellMask]; 

sin resultados.

Se puede ver el comportamiento incorrecto en las imágenes adjuntas:

botón de bisel superponer una ventana HUD:
Bevel button overlaying a HUD window

incorrecto botón de bisel de fondo: Botón
Wrong Bevel button background

Respuesta

2

Creación

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

botón Agregar a la ventana

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

No está claro para mí cuál es esta NSWindow myWindow. ¿Es el contenedor de botones? En este caso, su contenido rect es lo que definió como NSRect rect? – loretoparisi

+0

¿Está claro ahora? –

+0

¡Suena genial! – loretoparisi

25

Dependiendo de su situación, esto también puede trabajar:

cambiar el estilo del botón de bisel o Square, el modo se debe ajustar a "cambio momentáneo" y Frontera , Transparente, Mixto y Seleccionado debe estar DESACTIVADO. Así es como arreglé el problema de fondo blanco en mis botones.

+0

Este es un problema en Mac OS 10.12 pero no en 10.13. – samatron

1

Debe establecer el tipo de botón: myButton.buttonType = NSMomentaryChangeButton;

4

he hecho el trabajo mediante el establecimiento de 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) 

De esta manera el botón se oscurece cuando se pulsa, pero ningún cuadrado aparece fea. Probado solo en El Capitán).

+0

también puede agregar 'btn.alternateImage = myButtonAlternateImage' y se mostrará cuando se presione el botón –

Cuestiones relacionadas