It's not clear if you want to center the content rect and then build the frame rect to keep the content rect centered, or if you want to center the frame rect and are interested in the corresponding content rect.
In either case, NSWindow
has methods that will help. Before you have an instance, you can use the class methods +frameRectForContentRect:styleMask:
and +contentRectForFrameRect:styleMask:
. Those take into account the window style as expressed by the style mask, but do not take any toolbar the eventual window may have into account.
If you're working with an existing instance, you can use the methods -frameRectForContentRect:
and -contentRectForFrameRect:
. Those use the current style of the window and take its toolbar into account, too. (The toolbar is within the frame rect but not the content rect.)
You seem determined to use the actual center of the screen for the window. However, you should consider using the -center
method of NSWindow
. It positions the window horizontally centered but actually higher than the true vertical center of the screen. It does that deliberately since that's deemed more prominent and immediate for the user.
NSWindow.center()
to accomplish this without complex calculations of frame size and titlebar height – Stodder