Position window before becoming visible

When running my app again, I load previously loaded docs (NOT based on NSDocument) and create their window controllers. I then need to position them relative to whatever the current screen situation is, which might have changed since the last time they ran. This should happen before the window is shown.


I previously had the positioning code happen at the end of the custom window controller's windowDidLoad method. Many custom views have to update their constraints and such before the window knows its final size. The window size would always end up being wrong by the time it was shown. I could force it to layout some time later and it would be correct. I even had this at the top of my window positioning method, but it really didn't fully lay everything out enough for the window to know its correct size:


  [self updateConstraintsIfNeeded];
  [self layoutIfNeeded];
  // Looks like I need to force it to layout a 2nd time. Some Apple documentation alludes to this case:
  [self layoutIfNeeded];


What I ended up doing was to move the positioning code out of windowDidLoad and put it at the end of my custom window controller's makeWindowControllers method, right after I call makeKeyAndOrderFront on the window. However, this means the window will become visible in the default location, then quickly move to the final postion.


My question: What's a better way to position a window after it loads and before it becomes visible? This needs to work on 10.10 and greater.

Replies

It is OSX App, right ?


If I understand well, I think I had the same problem of this interim window position and then move.


So, I finally did the layout (in fact setFrame) in windowDidBecomeMain

I also added a property in the windowController

firstAppearance: Bool

to track if that was the initial show of the window, to avoid side effect when moving inactive window.