Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing NSWindow to fit child NSView

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?

like image 276
Cristian Avatar asked Apr 16 '12 16:04

Cristian


1 Answers

-[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];
like image 68
hamstergene Avatar answered Oct 28 '22 11:10

hamstergene