Centering Windows on screen

2019-04-28 23:15发布

Short and sweet: How can I tell Interface Builder to center a window on a user's screen? I've seen the positioning tool on the inspector, but eyeballing doesn't always land as squarely as I like. Is this something where I should switch over to Xcode and add something to the init or awakeFromNib methods?

4条回答
看我几分像从前
2楼-- · 2019-04-28 23:29

In your appdelegate, in applicationfinishedlaunching, add this code at the bottom.

[window center];
查看更多
一纸荒年 Trace。
3楼-- · 2019-04-28 23:30

Use [window center]; for the centre of the window to be centred to the screen. Also, assuming you're in 10.5+ , there is a centre to screen button in InterfaceBuilder

查看更多
淡お忘
4楼-- · 2019-04-28 23:47

For macOS Mojave this worked for me, in AppDelegate applicationDidFinishLaunching for the Main Window:

[[[NSApplication sharedApplication] mainWindow] center];
查看更多
Juvenile、少年°
5楼-- · 2019-04-28 23:50

You could use [window center] as answered by wahkiz, but that might not be exactly what you want. The documentation states that this will put the window centered horizontally, but somewhat above the vertical centre. In this way the window has a visual prominence.

If you want to put your window in the exact centre, then manual positioning (as commented by Koning Baard XIV) will work for you.

查看更多
登录 后发表回答