On a program I am currently working on, I would like to lock the aspect ratio of the main window to make sure some visual elements stay proportionate.
I have managed to get this working by doing the following in the AppDelegate class:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[window setAspectRatio:NSMakeSize(1.48, 1.0)];
}
Whenever the user drags the window, the aspect ratio is locked as I would expect, but there is one issue. I have specified a window size in IB of 592×400, which does conform to the aspect ratio, but when I first try and resize the window while the application is running the window sort of quickly snaps to a new ratio. I am guessing that setAspectRatio: could be taking into the bar at the top of the window, and therefore messing up the calculation.
Does anybody know a decent workaround to this problem, or any alternative approach?
Also, is there an alternative way of resizing a window programatically that will respect the aspect ratio set by setAspectRatio?
Before, when changing the size programatically I have been using:
[window setContentSize:NSMakeSize(592, 400)];
Which I have tested to confirm that it ignores the specified aspect ratio, as would be expected!
Thanks!
The size in IB is the content size – it doesn’t include the 22 points for the titlebar (assuming a standard titlebar). What you want is to use
setContentAspectRatio:instead.As for programmatic resizing, there’s no API for that. You should just decide which dimension is your reference and multiply or divide by the content aspect ratio appropriately to get the other, then
setContentSize:as before.