xcode 显示后调整 NSPopover 的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14449945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:34:53  来源:igfitidea点击:

Resize NSPopover after it is shown

xcodecocoanspopover

提问by Chris

I have an NSPopover who's content in an NSView. I create the popover and add the view like this:

我有一个 NSPopover,它包含在 NSView 中的内容。我创建弹出窗口并添加这样的视图:

NSPopover *thePopover = [[NSPopover alloc] init];
[thePopover setContentViewController:myViewController];

I then show the popover and set the size:

然后我显示弹出窗口并设置大小:

[thePopover setContentSize:NSMakeSize(300.0f, 160.0f)];

At this point the popover is visible and its content view is correct. Is there a way that I can resize the popover at this point without closing and re-showing it?

此时,popover 是可见的,其内容视图是正确的。有没有一种方法可以在此时调整弹出框的大小而无需关闭并重新显示它?

回答by Chris

I was able to achieve this by calling back to the controller that launched the popover via a delegate protocol and resetting the size using the setContentSize:

我能够通过回调通过委托协议启动弹出框的控制器并使用 setContentSize:

回答by Peter Hosey

From the documentation:

文档

The popover's content size is set to match the size of the content view when the content view controller is set.

当设置内容视图控制器时,弹出窗口的内容大小设置为匹配内容视图的大小。

So, set the frameof the popover's content view.

因此,设置弹出内容视图的框架

回答by Valentin Shergin

NSWindowhas private property _popoverthat store a reference to NSPopover. You can use it to access to NSPopoverinstance from any NSViewController(and then call setContentSize:on it) like this:

NSWindow具有_popover存储对 的引用的私有属性NSPopover。您可以使用它NSPopover从任何NSViewController(然后调用setContentSize:它)访问实例,如下所示:

- (NSPopover *)popover
{
    NSWindow *window = self.view.window;
    NSPopover *popover = [window valueForKey:@"_popover"];
    return popover;
}