0
votes

I've been really struggling with this one.

I have an application which has a custom view. When the application starts, I set the custom view's frame size to the application window's frame size. This works. In the AppDelegate.m, I am loading another view controller's view into the custom view. In the view of the view controller, I have an image which is placed at the center of the screen programatically. I have a windowDidResize method in my AppDelegate.m which gets called when I resize the application's window.

The problem is when I resize the application's window, the image does not get repositioned in the center of the main window. I tried using NSWindowDelegate on both the loaded view and the view controller with coding a windowDidResize: method, but this also does not work.

Please help! Thanks.

1
Have you tried setting up constraints ? - GoodSp33d
I cannot use constraints because I want to position objects on the view dynamically as well. - Anjaan
For posterity, I had a similar issue; in this case, I would suggest first looking at autoresizingMask. Programmatically, windowDidResize is probably the wrong place since it is intended for clean up operations, especially with live resizing. For custom view resizing, look at resizeWithOldSuperviewSize. - Jerry

1 Answers

0
votes

Probably a hack, but what I do is capture the initial window frame etc size as an ivar at an 'init' method; I then use this to scale any x/y/height/width measuring at run-time.

//  Handy scaling macros
#define X(xVal)         ((xVal) * (x_scale))
#define Y(yVal)         ((yVal) * (y_scale))

Each window's delegate handler for size changes:

- (void)set_scales:(NSSize)size
{
    self.x_scale = (CGFloat)(size.width)  / (CGFloat)(orig.width);
    self.y_scale = (CGFloat)(size.height) / (CGFloat)(orig.height);
}

and those windows have their def/undef of x/y_scale macros will simply return these ivars. So all told, each window has a NSSize orig, CGFloat x,y, a scale delegate method, two macros (x/y_scale) and two global macros X,Y.

Then whenever drawing etc, use the X() and Y() macros to position and affect h/w. i.e.,

[@"foo" drawAtPoint:NSMakePoint(X(100),Y(200)) withAttributes:nil];