Search code examples
nsdocument

NSDocument - setting a reasonable starting location for the window


I'm playing about with the most recent NSDocument in a Swift document-based-app. One thing that's a bit odd is that the starting location for a new window is near the bottom of the screen.

Playing with the Storyboard a bit, its not clear how to use the built-in settings to come up with a reasonable "near the top" selection - the setting moves up from the bottom, not down from the top, so the position would change depending on the screen size?

I assume there's a position mechanism I can hook, but it's not obvious in the shell code that's supplied. Any hints?


Solution

  • OS X coordinate system is flipped in contrast to iOS. So the 0,0 is the bottom left corner.

    You can calculate the position of your window in similar manner (any screen size)

      CGFloat width = NSWidth([self.window screen].frame); 
      CGFloat height = NSHeight([self.window screen].frame);
      [self.window setFrame:NSMakeRect(100, height - 100, width, height) display:YES];
    

    Most easiest is to set initial height to 900 and forget about it and enable window restoration -> this will cause to open the window where it previously was and this is where it user wants.

    Select your window in Storyboard. And fill initial position coordinates initial position