I don't have much experience with programming for Mac, I come from a Windows background.

So, I use this code to show an NSOpenPanel, but I also want to specify the position and size on screen of the dialog when it appears. How do I do that?

NSOpenPanel *openPanel  = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setCanChooseDirectories:YES];
[openPanel setCanChooseFiles:NO];
NSInteger result    = [openPanel runModalForTypes:nil];

Edit


It seems that by calling

[openPanel setFrame:NSMakeRect(0, 0, 500, 500) display:YES];

I can set the size of NSOpenPanel (it becomes 500x500 pixels), but not its upper-left corner, it's centered to the screen, instead of appearing at the top-left corner of the screen.

有帮助吗?

解决方案

You'll have to subclass NSOpenPanel and override the center method, which gets called before the panel is placed on screen. Here's how you would put it in the top right corner:

- (void)center {

    NSRect myFrame = [self frame];
    NSRect screenFrame = [[self screen] visibleFrame];

    myFrame.size.height = round(screenFrame.size.height / 2);
    myFrame.origin.x = screenFrame.origin.x + screenFrame.size.width - myFrame.size.width;
    myFrame.origin.y = screenFrame.origin.y + screenFrame.size.height - myFrame.size.height;

    [self setFrame:myFrame display:YES];
}

其他提示

NSOpenPanel inherits from NSWindow. I've never tried it, but it seems like you should be able to use the normal NSWindow methods to set its size and location.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top