NSButton default button with blueish look

后端 未结 5 860
感情败类
感情败类 2021-02-03 23:36

I have a dialog with 1 or more buttons and want to be able to programmatically set the rightmost one be the default one, so that when the user presses the Enter or Return key it

相关标签:
5条回答
  • 2021-02-03 23:54

    The documentation says:

    Note that if you set the key equivalent to Return, that button becomes the default button.

    Thus, you're doing it correctly. You could try this after setting the key equivalent:

    [button setNeedsDisplay:YES];
    

    This forces the button to redraw itself.

    0 讨论(0)
  • 2021-02-04 00:03

    My setting has been a custom sheet where I wanted the default button to appear like within an NSAlert. The following configuration gives me the blue default appearance. Works in 10.12.3 Sierra.

       button.bezelstyle = NSBezelStyleRounded; // Push in Interface Builder
       button.keyEquivalent = @"\r";
       button.highlighted = YES; // this is the most important part.
    
    0 讨论(0)
  • 2021-02-04 00:04
    [myWindow setDefaultButtonCell:[btn cell]]; // should do the trick.
    

    See Apple's documentation for more details.

    0 讨论(0)
  • 2021-02-04 00:12

    I was missing to set the button bezel style properly and that's why it didn't look properly. I'm now setting the button as this:

    [btn setBezelStyle:NSRoundedBezelStyle];
    

    Then any of these work to set the default button:

    [myWindow setDefaultButtonCell:[btn cell]]; 
    

    or

    [btn setKeyEquivalent:@"\r"] 
    
    0 讨论(0)
  • 2021-02-04 00:13

    For Swift 4.2 just use

    button.isHighlighted = true
    
    0 讨论(0)
提交回复
热议问题