Resizing window with aspect ratio and a fixed border

Discussion in 'Mac Programming' started by rossipoo, Nov 3, 2011.

  1. macrumors regular

    Jun 7, 2009
    I am trying to make my window resizable with an aspect ratio, and have a fixed border. I've tried a few different methods: the aspectRatio, and contentAspectRatio properties of NSWindow, an NSLayoutConstraint to set the height of a subview proportional to its width, and a custom windowWillResizeToSize method.

    The aspectRatio and contentAspectRatio properties can't account for a fixed border. The aspect ratio will be different when the window is small than when it is large.

    windowWillResizeToSize works only for diagonal resizing, and when resizing the window SMALLER horizontally and vertically. There is no way to tell what corner or side is being resized, and even if that were possible, it's not appropriate to return a size larger than the proposed size when doing a horizontal or vertical resize.

    NSLayoutConstraint seems to be the best in theory, but it seems to be broken when resizing horizontally from the sides of the window, resizing is just stuck when resizing horizontally. Corners and vertical resizing does resize the window as expected.

    This is my code for the constraint. Does anyone have any suggestions?

    	NSLayoutConstraint *constraintHeight = [NSLayoutConstraint constraintWithItem:_customView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_customView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
  2. thread starter macrumors regular

    Jun 7, 2009
    To clarify, here is a diagram of the window. I want the highlighted subview to be maintained at a certain aspect ratio, and I also want to have a fixed border around the content that is not part of the aspect ratio calculation.

    As indicated, when I am using the NSLayoutConstraint code above, the window will resize vertically by dragging the top and bottom edges of the window, but dragging the side edges results in nothing happening. I have tried turning the constraint around: height=width to width=height, and I have tried using both together, but the behavior is the same regardless.

    Could this be a bug with the constraint system?


    Uploaded with
  3. pieceOfHumans, Dec 17, 2011
    Last edited: Dec 17, 2011

    macrumors newbie

    Oct 23, 2011
    I have noted many -did routines in Mac OSX are predictably unreliable. The unreliability of them also changes from upgrade to upgrade in attempt to fix the issues. I steer away from them as only CoreData seems to be solid.

    Try using NSWindow-setContentAspectRatio:

    Good Luck
  4. macrumors regular

    Jun 28, 2003
    The following works perfectly for me:

    - (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize {
    	return NSMakeSize(frameSize.width, frameSize.width);
    If you need to figure out whether the user is resizing horizontally, vertically, or both, compare [sender frame].size with frameSize.
  5. thread starter macrumors regular

    Jun 7, 2009
    Actually, you can't tell what direction the user is resizing by comparing the frame. There is no way to differentiate between the user dragging from the bottom edge straight down, and from one of the corners straight down.

Share This Page