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?
-[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