【问题标题】:NSButton white background when clicked单击时 NSButton 白色背景
【发布时间】:2011-12-07 01:33:19
【问题描述】:

使用自定义图像和备用图像创建 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 窗口:

斜角按钮背景错误:

【问题讨论】:

    标签: cocoa nsbutton nsbuttoncell


    【解决方案1】:

    我通过将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 中测试)。

    【讨论】:

    • 你也可以添加btn.alternateImage = myButtonAlternateImage,按下按钮就会显示
    【解决方案2】:

    您应该设置按钮类型: myButton.buttonType = NSMomentaryChangeButton;

    【讨论】:

      【解决方案3】:

      根据您的情况,这也可能有效:

      将按钮的样式更改为斜角或方形,模式应设置为“瞬时更改”,边框、透明、混合和选定应关闭。这就是我修复按钮上的白色背景问题的方法。

      【讨论】:

      • 这是 Mac OS 10.12 上的问题,但不是 10.13 上的问题。
      【解决方案4】:

      创建按钮

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

      【讨论】:

      • 我不清楚这是哪个 NSWindow myWindow。它是按钮容器吗?在这种情况下,它的内容 rect 就是您定义为 NSRect rect 的内容?
      • 我尝试了这个解决方案,替换一个窗口的 contentView 解决了 NSButton 的白色背景问题,但它引入了另一个问题,关键窗口焦点。这发生在一种情况下:单击关闭按钮但未释放它时(因此仅执行按钮按下事件)。在这种情况下,处理按钮的 NSWindow 获得焦点,下面的窗口(以及其他窗口将失去焦点。所以问题是用户必须在另一个窗口(在我的情况下为 HUD)上单击两次才能获得焦点。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多