문제

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