10

I was wondering if anyone knows, how do I read when NSWindow is being resized? Let's imagine I have a button in an empty window (other than the button), then user resizes the window from the bottom right corner, now I should make it so that the button also resizes when the window is being resized. I know how to resize button, and I know how to resize a window, and I know lots of stuff, but I don't know how to get notificated when ever the user resizes the window, any tips?

  • Also, example is always the best answer, and giving a great example will totally make your answer the correct one, I guess it has something to do with NSWindowDelegate but not sure how to use it... :S –  Jul 11 '11 at 15:28
  • Did you figure out how to parse the notification to get width and height? – Adam Johns Apr 27 '16 at 14:46

3 Answers3

12

Can you use the ‑windowDidResize: delegate method?

Hejazi
  • 16,587
  • 9
  • 52
  • 67
Marm0t
  • 921
  • 9
  • 18
5

in the awake from nib of your .m file write

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenResize) name:NSWindowDidResizeNotification object:nil];

and create a method now

(void)screenResize
{
    NSRect rect = Preloader.frame;
    rect = NSMakeRect(self.view.frame.origin.x+self.view.frame.size.width/2, self.view.frame.origin.y+self.view.frame.size.height/2, Preloader.frame.size.width, Preloader.frame.size.height);
    Preloader.frame = rect;
    NSLog(@"X = %f, Y = %f, W = %f, H= %f", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
}

and when you get out of that class write

    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:nil];
mariusLAN
  • 1,195
  • 1
  • 12
  • 26
NSparesh
  • 21
  • 1
  • 1
-2

On Xcode 4.3 and later, use the autolayout to add constraints to the button, you can get very complex layouts without writing any line of code .

mLar
  • 2,967
  • 2
  • 22
  • 23
  • this isn't very helpful, and not to the point either. Sorry for downvote, but you should change your answer – Citrus Aug 04 '15 at 17:39
  • I disagree; autolayout is is correct approach to resolving this issue. – geowar Feb 17 '16 at 21:38
  • they were asking about a resizing event, which is best handled in code. and in my opinion Apple still has a long way to go with their interface builder... – quemeful Feb 24 '16 at 11:54