Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initial NSWindow size as percentage of screen size

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.

like image 755
Yoav Avatar asked Dec 15 '22 04:12

Yoav


1 Answers

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];
like image 66
Mick MacCallum Avatar answered Dec 27 '22 12:12

Mick MacCallum