0
votes

I'm having trouble resizing an NSWindow to fit an NSView. It must be a logic error as it works but not for one action.

I have one NSWindow which is empty, and 3 NSViews with components and are different sizes.

With the following code I resize the NSWindow to fit the NSView and display it:

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

This code works fine.

However in one NSView I have a Back button, and while this displays the correct NSView in the NSWindow, it does not re-size it back. As an example the initial window is a certain size, I click to switch to another view and it resizes correctly, I press the back button, the NSView is displayed but the window stays the same size?

Can anyone explain to me why when I switch back to the original NSView, it doesn't resize the NSWindow?

Thanks in advance everyone. This is the complete code I have:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [_window setContentSize:_mainView.frame.size];
    [_window setContentView:_mainView];
}

- (IBAction)switchSubtractionView:(id)sender {
    [_window setContentSize:_subtractionView.frame.size];
    [_window setContentView:_subtractionView];
}

- (IBAction)switchAdditionView:(id)sender {
    [_window setContentSize:_additionView.frame.size];
    [_window setContentView:_additionView];
}

// -----------------------------------------------------------------------
// THE FOLLOWING METHOD DISPLAYS THE NEW VIEW CORRECTLY BUT DOESN'T RESIZE
// -----------------------------------------------------------------------
- (IBAction)switchMainMenu:(id)sender {
    [_window setContentSize:_mainView.frame.size];
    [_window setContentView:_mainView];
}

Thanks in advance everyone.

EDIT: it seems to me that when getting VIEW.frame.size, if this is repeatedly used, it loses its values? This seems very strange behaviour to me?

1

1 Answers

3
votes

A window's content view must always be sized to fill the window's content area. Therefore, when you set the window's content size, you effectively change the size of the current content view. This happens just before you switch the content view, so you are changing the size of the old view.

Try setting the content view to a new, disposable NSView before changing the content size, and then setting the new content view.