Resizing nspopover

老子叫甜甜 提交于 2019-12-23 02:45:57

问题


I am trying to resize an NSPopover prior to displaying it by setting the frame of the NSView it contains using the method:

setFrameSize:

However, when I try to display the popover by calling:

showRelativeToRect: ofView: preferredEdge:

The view returns to its previous size. Why is this happening? Is there another way I should be sizing the popover?

Thanks in advance, Ben.


回答1:


There's a setContentSize on the popover, but I have the impression this doesn't work so well. I mean it works, but resizes the content (including child views). So the better way is to create a new NSViewController and assign it the new view. On setting this new controller on the NSPopover the size is properly respected and if the popover is currently shown it will animate to the new view.

If you can't do this but instead want to resize the content in place use something like this:

NSRect frame = valuePopupList.frame;
frame.origin = NSMakePoint(2, 2);

... determine new frame ...

NSSize contentSize = frame.size;
contentSize.width += 4; // Border left/right.
contentSize.height += 4; // Ditto for top/bottom.
if (contentSize.height < 26) {
    contentSize.height = 26;
}
valuePopupList.frame = frame;
statementsPopover.contentSize = contentSize;

[statementsPopover showRelativeToRect: area ofView: view preferredEdge: NSMaxXEdge];
valuePopupList.frameOrigin = frame.origin;



回答2:


You should implement -preferredContentSize in your NSViewController that is contained by the popover. That seems to be the most reliable way of controlling the popover size.



来源:https://stackoverflow.com/questions/14378949/resizing-nspopover

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