When would you need to open a SwiftUI view in a new window? One situation would be showing something secondary in a window in a document-based app with a new window for each document. SwiftUI has Window to open a separate window, but the window is shared by every open document in the app.

Recently I was updating a SwiftUI app to use NavigationSplitView. In the original version of the app, I had a NavigationView with three columns. The right column showed an image browser. A three-column NavigationSplitView wouldn’t work because there was nothing in the second column to select to open the image browser so I wanted to open the image browser in a new window.

I wasn’t sure if it was possible to get what I wanted: open the image browser in its own window. Searching online I found a GitHub gist that did what I wanted. I’m sharing what I learned in this article.

To open a SwiftUI view in a new window, you must do the following:

  • Add a function as an extension to the View protocol that creates a NSWindow and sets its content view to a SwiftUI HostingView.
  • Add a NSWindowDelegate subclass and a function to prevent the app from crashing when closing the window.
  • Open the view in a new window by calling the function to create a new window.

Add an Extension to View

To open a SwiftUI view in a new window, you must add an extension to the View protocol and write a function that creates a new NSWindow instance, configure it, and set its content view to a SwiftUI HostingView.

public extension View {
  func openInNewWindow(width: Int = 1024, height: Int = 768, _ introspect: @escaping (_ window: NSWindow) -> Void) {
    let windowDelegate = WindowDelegate()
    let window = NSWindow(
      contentRect: NSRect(x: 0, y: 0, width: width, height: height),
      styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
      backing: .buffered,
      defer: false
    )
    window.isReleasedWhenClosed = false
    window.title = "Window"
    
    window.delegate = windowDelegate
    window.contentView = NSHostingView(rootView: self)
    introspect(window)
    window.center()
    window.makeKeyAndOrderFront(nil)
  }
}

I added the width and height arguments to make the function more flexible. The code listing shows what I used in my app. You may need to configure your window differently.

Add a NSWindowDelegate Subclass

To avoid crashes when the window closes and avoid memory leaks, you must create a NSWindowDelegate subclass and add a function to run when the window closes.

fileprivate final class WindowDelegate: NSObject, NSWindowDelegate {
  func windowShouldClose(_ sender: NSWindow) -> Bool {
    NSApp.removeWindowsItem(sender)
    return true
  }
  
  deinit {
    NSLog("deallocated ...")
  }
}

Open the View in a New Window

Call the function openInNewWindow for the view and supply a closure to do anything you want to configure the window. The following example sets the window title:

ImageBrowser().openInNewWindow { window in
  window.title = "Images"                
}