Reading NSWindow Change Events

I was wondering if anyone knows how I can read when NSWindow changes? Imagine that I have a button in an empty window (except for the button), then the user resizes the window in the lower right corner, now I have to make the button also change when the window is resized. I know how to resize a button, and I know how to resize a window, and I know a lot of things, but I don't know how to get a notification when a user resizes a window, any tips?

+8
event-handling resize cocoa nswindow
source share
3 answers

Can you use the delegate method ‑windowDidResize: :?

+9
source share

starting with nib of your .m file write

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

and create a method

 (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 exit this class write

  [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:nil]; 
+2
source share

In Xcode 4.3 and later, use autolayout to add restrictions to the button, you can get very complex layouts without writing a single line of code.

-one
source share

All Articles