Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rossipoo

macrumors regular
Original poster
Jun 7, 2009
109
0
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?

Code:
	NSLayoutConstraint *constraintHeight = [NSLayoutConstraint constraintWithItem:_customView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_customView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
 

rossipoo

macrumors regular
Original poster
Jun 7, 2009
109
0
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 ImageShack.us
 

pieceOfHumans

macrumors newbie
Oct 23, 2011
1
0
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
 
Last edited:

Kenndac

macrumors 6502
Jun 28, 2003
256
63
The following works perfectly for me:

Code:
- (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.
 

rossipoo

macrumors regular
Original poster
Jun 7, 2009
109
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.