8

カスタム イメージと代替イメージを使用して Cocoa ベベル ボタンを作成すると、奇妙な動作が発生します。押された状態では、ボタンの背景が白くなります。透明なウィンドウ (HUD ウィンドウ) のサブビューとしてボタンを追加しています。

私が知っているすべてのテクニックを試しています:

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

私も試しました

[closeButton setButtonType:NSMomentaryChangeButton];

[[closeButton cell] setHighlightsBy:NSContentsCellMask];

結果なし。

添付のスクリーンショットで間違った動作を確認できます。

HUD ウィンドウをオーバーレイするベベル ボタン:
HUD ウィンドウをオーバーレイするベベル ボタン

間違ったベベル ボタンの背景:
間違ったベベル ボタンの背景

4

4 に答える 4

25

状況によっては、これも機能する場合があります。

ボタンのスタイルをベベルまたはスクエアに変更し、モードを「一時的な変更」に設定し、ボーダー、透明、混合、および選択をオフにする必要があります。これが、ボタンの白い背景の問題を修正した方法です。

于 2012-02-26T14:31:08.817 に答える
4

に設定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)

この方法では、ボタンを押すと暗くなりますが、醜い四角形は表示されません。El Capitan でのみテスト済み)。

于 2016-02-23T21:16:03.850 に答える
2

ボタンの作成

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

ウィンドウにボタンを追加

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];
于 2011-10-13T17:54:11.173 に答える