Centering NSWindow in Cocoa
26
Oct1
Oct1
I was messing around with Cocoa. Building some dummy app to try out GData framework. I was creating preferences window and each time I ran the application – preferences window was showing somewhere not in the center of the screen. Yes, I know I can adjust the position via Size inspector for the window. Still, the adjustment area is so small I couldn’t adjust it correctly. And my OCD wanted pixel-perfect positioning. iGoogled and all I found was this NSWindow instance method:
[window center];
Note: it does not show your window on screen. This method only sets it’s frame to the center of the main (read – the one that has menu bar on top) screen.
Enjoy this article?
Consider subscribing to our RSS feed!
12:54 on October 26th, 2009
Nice trick! I’ll have to remember that one!