Prior to macOS 15 “Sequoia”, if you tried centering a NSWindow containing a NSHostingController or NSHostingView that hosts a SwiftUI view, everything would work just fine.
let window = NSWindow(...)
window.contentViewController = NSHostingController(...)
window.center()
New in Sequoia, view layout appears to be computed more lazily than before. This means that by the time the call to center()
occurs, the final size of the window is not yet known, leading to a window that is not actually centered.
To fix this, we found that adding a call to updateConstraintsIfNeeded()
fixes things.
let window = NSWindow(...)
window.contentViewController = NSHostingController(...)
window.updateConstraintsIfNeeded() // macOS 15+
window.center()
If you have a Mac app that centers NSWindows containing SwiftUI views, you may want to ensure that on macOS 15, they are actually centered.