Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何设置NSOpenPanel对话框的位置和大小?_Objective C_Cocoa_Nsopenpanel - Fatal编程技术网

Objective c 如何设置NSOpenPanel对话框的位置和大小?

Objective c 如何设置NSOpenPanel对话框的位置和大小?,objective-c,cocoa,nsopenpanel,Objective C,Cocoa,Nsopenpanel,我没有太多的Mac编程经验,我来自Windows背景 因此,我使用这段代码来显示NSOpenPanel,但我还想指定对话框出现时在屏幕上的位置和大小。我该怎么做 NSOpenPanel *openPanel = [NSOpenPanel openPanel]; [openPanel setAllowsMultipleSelection:NO]; [openPanel setCanChooseDirectories:YES]; [openPanel setCanChooseFiles:NO];

我没有太多的Mac编程经验,我来自Windows背景

因此,我使用这段代码来显示NSOpenPanel,但我还想指定对话框出现时在屏幕上的位置和大小。我该怎么做

NSOpenPanel *openPanel  = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setCanChooseDirectories:YES];
[openPanel setCanChooseFiles:NO];
NSInteger result    = [openPanel runModalForTypes:nil];
编辑
看来打电话

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

我可以设置NSOpenPanel的大小(变成500x500像素),但不能设置它的左上角,它位于屏幕中央,而不是显示在屏幕的左上角。

NSOpenPanel继承自NSWindow。我从未尝试过,但似乎您应该能够使用正常的NSWindow方法来设置其大小和位置。

您必须将NSOpenPanel子类化,并覆盖
中心
方法,该方法在面板显示在屏幕上之前被调用。下面是如何将其放在右上角:

- (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];
}

非常感谢,不幸的是,在我听从你的建议之前,我需要一些时间来阅读继承的ObjjcTy-C,语法似乎有点棘手,这与C++有很大的不同。不幸的是,子类不能很好地使用沙箱应用程序。“…不是沙盒支持的子类”