Loading...

How to open a window in SwiftUI using NSWindowController?

question swiftui swift
Ram Patra Published on May 29, 2022

Although many things in SwiftUI are idiomatic and straightforward, showing your view in a new window needs a bit of coding to do. Hence, this short post.

First, let’s create a Window Controller that will hold your window/view:

import AppKit
import SwiftUI

class WindowController<RootView: View>: NSWindowController {
    convenience init(rootView: RootView) {
        let hostingController = NSHostingController(rootView: rootView.frame(width: 400, height: 400))
        let window = NSWindow(contentViewController: hostingController)
        window.setContentSize(NSSize(width: 400, height: 400))
        self.init(window: window)
    }
}

What we’re doing here is creating a Hosting Controller (which is a subclass of View Controller) that takes your supplied View, does its magic, and then gives it to NSWindow.

Now, let’s create two Views in SwiftUI, Parent and Child, like below:

import SwiftUI

struct NewWindowParentView: View {
    @State private var windowCount = 1
    
    var body: some View {
        Button("Show new window") {
            let windowController = WindowController(rootView: NewWindowChildView())
            windowController.window?.title = "Child Window \(windowCount)"
            windowCount += 1
            windowController.showWindow(nil)
        }
    }
}
import SwiftUI

struct NewWindowChildView: View {
    var body: some View {
        Text("Child view")
    }
}

Parent View resemble whatever your main View in your app is and the Child View is the View that you want to show in a new Window.

That’s it. So, when you open the Parent View, you will see a Button and every time you press this button, a new Window will be created showing your Child View.

Lastly, the entire example code can be found on GitHub.

Note: Apple has now added better ways to open windows programmatically in SwiftUI.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on May 29, 2022
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui swift September 13, 2024 How to add a character limit to a TextField in SwiftUI?

To add a character length limit to a TextField in SwiftUI, you can use a combination of Swift’s .onChange, .onPasteCommand modifier, and string manipulation to limit the number of characters the user can enter.

question swiftui swift August 31, 2024 @Published in SwiftUI

In SwiftUI, the @Published property wrapper is used in combination with the ObservableObject protocol to automatically announce changes to properties of a class. This allows SwiftUI views that depend on these properties to update automatically when the data changes.

question swiftui swift September 2, 2024 Combine in SwiftUI and how you can rewrite the same code using async await

Combine is Apple’s declarative framework for handling asynchronous events and data streams in Swift. Introduced in SwiftUI and iOS 13, Combine leverages reactive programming principles, allowing developers to process values over time and manage complex asynchronous workflows with clarity and efficiency.