swiftuinswindownswindowcontroller

Window Control Buttons Missing from New Window in SwiftUI App


Starting with a new SwiftUI macOS project in Xcode, I set up some very basic functionality.

@main
struct SwiftUIWindowTestApp: App {
  var body: some Scene {
    WindowGroup {
      ContentView()
    }
  }
}

//ContentView
struct ContentView: View {
  var body: some View {
    //Button to open a new window
    Button("Open Preferences"){
      openPreferences()
    }
    .padding(100)
  }
  
  //Open the new window
  func openPreferences(){
    let preferencesWindow = NSWindow()
    preferencesWindow.contentView = NSHostingView(rootView: PreferencesView())
    let controller = NSWindowController(window: preferencesWindow)
    controller.showWindow(nil)
  }
}

//PreferencesView
struct PreferencesView: View {
  var body: some View {
    Text("Preferences")
      .frame(width:300, height:200)
  }
}

On app launch, I see what I expect: enter image description here

But after I click Open Preferences, I see a new window appear like this: enter image description here Why are the control buttons (close, minimize, zoom) missing on that popup window?

I have tried explicitly setting the buttons to be visible (even though they should be by default) and nothing changes:

preferencesWindow.standardWindowButton(.closeButton)?.isHidden = false
preferencesWindow.standardWindowButton(.miniaturizeButton)?.isHidden = false
preferencesWindow.standardWindowButton(.zoomButton)?.isHidden = false

I have this same sample working fine in an AppKit app, so it appears to be something weird with SwiftUI. Any ideas?


Solution

  • Looks like NSWindow will create a window like that unless you set a hosting controller on it. This modified version of your code works for me:

    func openPreferences(){
        let preferencesWindow = NSWindow(contentViewController: NSHostingController(rootView: PreferencesView()))
        let controller = NSWindowController(window: preferencesWindow)
        controller.showWindow(nil)
      }