PDA

View Full Version : How to Center window




satyam90
Nov 20, 2007, 12:01 AM
Using Cocoa with Obj C and XCode 2.4.1
I created preferences window for my application. Its working fine. But I want to centre it every time it is displayed. I tried to position it at centre point thru show inspector of Interface builder. Still I couldn't. Need some help to centre the window.

Satya.



HiRez
Nov 20, 2007, 01:06 AM
Using Cocoa with Obj C and XCode 2.4.1
I created preferences window for my application. Its working fine. But I want to centre it every time it is displayed. I tried to position it at centre point thru show inspector of Interface builder. Still I couldn't. Need some help to centre the window.Call [myWindow center] on it when it's displayed. If you aren't the one controlling when it gets shown (by invoking a call on it directly that makes it visible), put the call inside a delegate method, such as perhaps windowDidBecomeMain: or windowDidExpose:

elppa
Nov 20, 2007, 02:11 AM
In another language, what I'd do is this:

[1] Take the width and the height of the users screen.
[2] Divide both by 2 to get a midpoint.
[3] Get the width and height of the window and half it.
[4] Offset it by the values collected in step 3 from the midpoint.

Sounds like Apple have a nifty shortcut though from the above poster.

gnasher729
Nov 20, 2007, 10:24 AM
In another language, what I'd do is this:

[1] Take the width and the height of the users screen.
[2] Divide both by 2 to get a midpoint.
[3] Get the width and height of the window and half it.
[4] Offset it by the values collected in step 3 from the midpoint.

And then test it on a system with two monitors.

satyam90
Nov 21, 2007, 06:30 AM
Call [myWindow center] on it when it's displayed.

I have a sharedinstance method which will return the instance of window and i am using
[[myWindow sharedInstance] center]
Still it is not working.

kainjow
Nov 21, 2007, 10:17 AM
I have a sharedinstance method which will return the instance of window and i am using
[[myWindow sharedInstance] center]
Still it is not working.

Make sure you're calling it after it's been initialized and awakeFromNib is called.