问题
In my application I've subclassed the NSWindow and set the window level as 25. Since the window level is 25 the alert box and error dialog box were being hidden by the window.
Is there any chance to set the level of NSAlert
回答1:
First of all. You shouldn't use magic numbers like 25.
There is a way to set window level but it's useless because runModal uses fixed windowLevel constant kCGModalPanelWindowLevel which is 8. You can verify it like this:
[self.window setLevel:25];
NSAlert *alert = [NSAlert alertWithMessageText:@"1" defaultButton:@"2" alternateButton:nil otherButton:nil informativeTextWithFormat:@"3"];
[alert runModal];
(lldb) po [alert.window valueForKey:@"level"]
8
#define NSModalPanelWindowLevel kCGModalPanelWindowLevel
Solution:
Use sheet
[alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse response){
}];
Swizzle implementation runModal with your own one.
Recreate NSAlert functionality as a subclass of NSWindow/NSPanel (don't inherit NSAlert) and call showWindow: if you need to display it.
来源:https://stackoverflow.com/questions/32837468/set-level-for-nsalert-in-cocoa