How to Center window

Discussion in 'Mac Programming' started by satyam90, Nov 19, 2007.

  1. macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #1
    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.
     
  2. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #2
    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:
     
  3. macrumors 68040

    elppa

    Joined:
    Nov 26, 2003
    #3
    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.
     
  4. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    And then test it on a system with two monitors.
     
  5. thread starter macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #5
    I have a sharedinstance method which will return the instance of window and i am using
    [[myWindow sharedInstance] center]
    Still it is not working.
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    Make sure you're calling it after it's been initialized and awakeFromNib is called.
     

Share This Page