Resizing NSWindow to fit child NSView

橙三吉。 提交于 2019-12-04 08:03:49

问题


I have one main NSWindow which is empty, and 5 NSViews. The NSViews have different buttons and labels etc, and the window is empty. The first view displayed is a menu, linking to the other views and back. This works fine and the views switch well.

However if the NSWindow is a certain size, and the NSView is bigger, then it spills out of the NSWindow and gets cut off.

Is there any way such that when I do:

[_window setContentView: theNewView];

to also have _window resize to fit the new view? If this is possible, can this be done with an animation?


回答1:


-[NSWindow setContentSize:] does this (without animation). Give it the desired size of the content view and it will resize both content view and the window appropriately, e.g.

[_window setContentSize:theNewView.frame.size];
[_window setContentView:theNewView];

For animation, you need to compute window size manually using frameRectForContentRect: and then change window's frame with animate:YES:

[_window setContentView:theNewView];
NSRect viewScreenFrame = /*translate theNewView.frame to screen coordinates*/;
NSRect wndFrame = [_window frameRectForContentRect:viewScreenFrame];
[_window setFrame:wndFrame display:YES animate:YES];


来源:https://stackoverflow.com/questions/10177882/resizing-nswindow-to-fit-child-nsview

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