Another newbie question (windowWillResize)

Discussion in 'Mac Programming' started by phanfave, Aug 8, 2008.

  1. phanfave macrumors newbie

    Jul 10, 2008
    I'm in chapter 6 of Hillegass's Cocoa book and working on the first challenge. It would have been easier if he provided how to get the window size (figured it out eventually). Here's my .m file:
    @implementation WindowController
    -(NSSize)windowWillResize:(NSWindow *)sender
    	NSSize currentSize = [sender frame].size;
    	framesize.width =currentSize.width;
    	framesize.height =currentSize.width * 2;
    	NSLog(@"framesize is %f wide and %f tall", framesize.width, framesize.height);
    	return framesize;
    When I try to resize the window I can't. I'm getting in my log I'm hitting this code. All I'm trying to do is keep a constant aspect ratio of 2 to 1 using this method. I've delegated to my .m object from the window. What I think is happening is that when I grab the corner to resize I'm stuck with both sizes as they are. I know this is something really easy to fix. I've been on this for an hour and could use a nudge. Thanks again!

  2. gnasher729 macrumors P6


    Nov 25, 2005
    windowWillResize is called while you use the mouse to resize the window. frameSize is the size that Cocoa wants to change the window size to based on your mouse movements. The size that you return is the size that you want the window to be.

    Here is what you are doing: You get the current width of the window. Then you return (current width, 2 * current width) as the window size that you want. So obviously the width of the window will never change; no matter where the user puts the mouse. Your code always tells Cocoa to leave the window size as it is, and to change the height to twice the width.

    What you need to do: You need to look at the value "frameSize" that is passed to the method. That is what Cocoa wants to change the size to. It is very unlikely that the method should ever look at the current size of the window. All you should do is to change frameSize.height = frameSize.width * 2; this will change the width according to the mouse position, and the height to twice the width.
  3. phanfave thread starter macrumors newbie

    Jul 10, 2008
    Thank you. When I read about windowWillResize I got the impression that this is a method that is called when the window size is changed by the user (this is correct). I also thought that is sending the new size of the window requested by the user. So if I resized from 100x200 to 200x400 I would pass 200x400 to the method and could change the frame size if I wanted to (sort of correct). But as you said since I was setting both width and height based on the current dimensions it's not going to change. I updated the code to change height as 2* width and now everything works ok. This is something I won't forget.

  4. yushen macrumors newbie

    Jul 2, 2009
    The above question seems to be clear to me.
    But I would like to further resize the view ( a scroll view actually) under the window accordingly.

    I have the delegate method set up correctly:
    - (NSSize) windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize
    but I cannot figure out the corresponding VIEW size as the proposedFrameSize of the WINDOW is changing.

    For an (very easy) example,
    the window sizes: 400*300, 500*400, etc.
    the desirable view size: 380*280, 480*380, etc. ( maintaining a space of 10 pixels at each of 4 sides)

    I read through everything about how to convert coordinate between different views (e.g. convertPoint:toView:) but I don't know how to change the view size correctly in this windowWillResize delegate method. (I also have the view added to the window).

    Your help is appreciated.

    - Yushen

Share This Page