Reputation: 6862
I have a NSWindow which is placed at the position of a NSStatusItem.
The window can change it's size using it's own method setSize:
, which just get's the right frame for the window like this:
- (NSRect)frameForSize:(NSSize)size {
NSRect newFrame = self.frame;
newFrame.origin.x += (newFrame.size.width - size.width) / 2;
newFrame.origin.y += (newFrame.size.height - size.height);
newFrame.size = size;
return newFrame;
}
This allows the window to stay centred, if the width is changed.
I can easily call this using code, but when I the user resizes it using the edges of the window, setFrame:display: is called directly.
There is a delegate method windowDidResize:notification:
, but if I set the frame there it get's set twice and it lags.
Is there an event that is being called before the window is actually resized? If not, how should I do this?
Upvotes: 0
Views: 1370
Reputation: 4553
According to your above comment, you've subclassed NSWindow
. Assuming that your method -frameForSize:
is on your NSWindow
subclass, override -[NSWindow setFrame:display:]
as follows:
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag
{
frameRect = (self.inLiveResize) ? [self frameForSize:frameRect.size] : frameRect;
[super setFrame:frameRect display:flag];
}
I've tested this. The window will resize smoothly and stay centered the entire time.
Upvotes: 2