Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get focus ring off an NSAlert button?

Is there any way that you can take away the focus ring of the NSAlert button. Here is what I mean:

Focus ring annoyingly appears on NSAlert button

Here is my code:

[NSApp activateIgnoringOtherApps:YES];
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:@"Quit"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Warning!"];
[alert setInformativeText:@"How do you solve this question?"];
[alert setAlertStyle:NSWarningAlertStyle];
[alert beginSheetModalForWindow:nil modalDelegate:self didEndSelector:@selector(someMethodDidEnd:returnCode:contextInfo:) contextInfo:nil];

2 Answers

Just been trying to solve this as well and came up with this workaround:

NSAlert *alert = [NSAlert alertWithMessageText:@"Alert" defaultButton:@"123" alternateButton:@"" otherButton:@"" informativeTextWithFormat:@""];
NSButton *button = [[alert buttons] objectAtIndex:0];

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [[button window] makeFirstResponder:nil];
});

[alert runModal];

The first two lines are straightforward - create an alert and get a button. Then the issue is to actually set the alert's panel's first responder to nil, however, it may be only after the alert has been run modal, so this is why the dispatch_after is used.

In your case, though, as you are using a sheet and not running it modal can use just this:

NSAlert *alert = [NSAlert alertWithMessageText:@"Alert" defaultButton:@"123" alternateButton:@"" otherButton:@"" informativeTextWithFormat:@""];
NSButton *button = [[alert buttons] objectAtIndex:0];

[alert beginSheetModalForWindow:window modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
[[button window] makeFirstResponder:nil];
like image 167
Charlie Monroe Avatar answered Nov 10 '25 09:11

Charlie Monroe


I would say you should not do this at all, this will go against apple guidelines.

like image 36
Anoop Vaidya Avatar answered Nov 10 '25 09:11

Anoop Vaidya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!