A temporary workaround is to still rely on the AppKit Lifecycle and to manage multiple NSWindow instances manually. You can use NSHostingControllers to load SwiftUI views into the window.
For example:
import Cocoa
import SwiftUI
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var primaryWindow: NSWindow?
func applicationDidFinishLaunching(_ aNotification: Notification) {
let hostingController = NSHostingController(rootView: SwiftUIView())
window = NSWindow(contentViewController: hostingController)
window?.toolbar = NSToolbar()
window?.title = "SwiftUI Test Window"
window?.makeKeyAndOrderFront(nil)
}
}
struct SwiftUIView: View {
var body: some View {
Text("Hello, World!")
.frame(minWidth: 640, maxWidth: .infinity, minHeight: 480, maxWidth: .infinity)
}
}
Just make sure the Swift UI view has some kind of declared or implicit minimum size or the window will be extremely small/practically invisible. More windows can be opened the same way, which is what I was trying to achieve.
I'd still like to figure out some kind of window management solution for SwiftUI projects. If anyone knows how to instantiate new windows directly through the SwiftUI lifecycle I'd love to know!
Post
Replies
Boosts
Views
Activity
@lupinglade Agreed. It's been really frustrating to try to emulate these changes without anything documented. I know it's all in beta, but a lot of the working sessions were showing them off as if these features already exist. It would be nice to get some sample code from those sessions.
Also having this issue. MacBook Pro (15-inch, 2019) on Big Sur 11.0.1 (20B28)/Xcode 12.2 (12B45b). I'd be shocked if Apple hasn't observed this internally. I opened a feedback request for this, FB8939085.
Fantastic workaround from @malc that doesn't require importing AppKit. Kudos!
This is still a bug in Xcode 12.5 on macOS 11.4. Have had to kill the CoreAudio daemon multiple times per day for nearly a year now. The simulator audio output control is new to me, have it set to an unused output when I don't need audio from simulator. Hopefully it holds. Thanks @Choonghee for the tip!