I'm using this code to show a panel :
- (void)displayPanelWithView:(NSView *)view andTitle:(NSString *)title {
[panel setTitle:title];
if ([windows count] < 1) {
[panel setContentSize:[view frame].size];
[panel setContentView:view];
[panel center];
[panel makeKeyAndOrderFront:nil];
}
else {
[panel setContentSize:[view frame].size];
[panel setContentView:view];
[NSApp beginSheet:panel modalForWindow:[windows objectAtIndex:0] modalDelegate:self didEndSelector:nil contextInfo:NULL];
}
}
panel
is an IBOutlet (it is instantiated in IB). It is a subclass of NSPanel.
windows
is an array of windows. If there are no windows, the panel is shown as a new window.
The problem
When I display a panel with a certain view, and windows
is not empty, the panel is shown as a sheet. The first time this happens, there is always an offset. What follows are screenshots of the first time the method is called, and the second time it is called (both with windows
not empty) :
This error can go away if I use this :
[panel setStyleMask:NSBorderlessWindowMask];
In that case the size is correct, BUT the sheet is not focused anymore and stays unfocused (so the blue button in the above screenshots is white all the time). What can I do to get the sheet to display right every time? What happens that first time that doesn't happen the next time?
I've managed to solve the issue by using this code instead :
As you can see, I add the view to the current content view of the panel instead of assigning it as the new content view of the panel.