Set level for NSAlert in Cocoa

南楼画角 提交于 2019-12-12 00:26:23

问题


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:

  1. Use sheet

    [alert beginSheetModalForWindow:self.window completionHandler:^(NSModalResponse response){

    }];

  2. Swizzle implementation runModal with your own one.

  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!