Reading NSWindow resize event
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 k开发者_JAVA技巧now 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?
Can you use the ‑windowDidResize:
delegate method?
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];
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 .
精彩评论