I would like to set my NSWindow size as a function of the screen size.
There is an option in Interface Builder to set it in points, but not as a function of the screen size.
How can I set it programmatically as a default?
Note that I still want UI preservation to restore its size from previous session if such state is available.
Well, you can get the screen's size, and just apply a little math to it to get this working. Set the dimensions of the window to the screens dimensions multiplied by the percentage of the screen you would like the window to take up. Going a step further, you can do this and keep the window centered by multiplying its origin by (1-percentage)/2.0.
NSRect screenSize = [[NSScreen mainScreen] frame];
CGFloat percent = 0.6;
CGFloat offset = (1.0 - percent) / 2.0;
[self.window setFrame:NSMakeRect(screenSize.size.width * offset, screenSize.size.height * offset, screenSize.size.width * percent, screenSize.size.height * percent) display:YES];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With