Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing NSWindow to fit new NSViews results in views being drawn incorrectly

Tags:

I have a NSWindowController with a setCurrentView method in which I use the following code to resize the window to the new view's size.

[self resizeWindowForContentSize: [newView frame].size];

NSView *contentView = [[self window] contentView];
NSDictionary *ani = [NSDictionary dictionaryWithObject:transition 
                                                forKey:@"subviews"];
[contentView setAnimations:ani];    
[[contentView animator] replaceSubview:currentView with:newView];

Edit: Added the resizeWindowForContentSize I found on CocoaDev.

- (void)resizeWindowForContentSize:(NSSize) size {
    NSRect windowFrame = [NSWindow contentRectForFrameRect:[[self window] frame]
        styleMask:[[self window] styleMask]];
    NSRect newWindowFrame = [NSWindow frameRectForContentRect: 
        NSMakeRect( NSMinX( windowFrame ), NSMaxY( windowFrame ) - size.height, size.width, size.height )
    styleMask:[[self window] styleMask]];
    [[self window] setFrame:newWindowFrame display:YES animate:[[self window] isVisible]];
}

The problem I'm having is that as you switch between views (using NSToolbar) the views begin to move around in the contentView. Over time they actually draw further and further away until eventually there is just a blank view.