swiftuivisionos

How can I overlay multiple views in SwiftUI so that they appear above the main content view


(for visionOS) What I want to do is position the pop-up windows so that they appear above the main ContentView, while ensuring that the ContentView remains visible. They don't pop up at once, so i want to do for example is place them all above the main ContentView. How can i do it? I have already enabled Multiple Windows. Let me be clear what I want to move is the WindowGroup.

@main
struct Demo17App: App {
    @Environment(\.scenePhase) private var scenePhase

    var body: some Scene {
        let viewModel = OptionsViewModel()

        WindowGroup {
            ContentView(viewModel: viewModel)
                .environmentObject(viewModel)
                
        }
        .windowStyle(.plain)

        WindowGroup(id: "firstWindow") {
            Text("Second Window Content [Information]")
                .frame(width: 300, height: 200)
                .background(Color.blue)
                .zIndex(100)
        }
        .handlesExternalEvents(matching: Set(arrayLiteral: "firstWindow"))
        .windowStyle(.plain)

        WindowGroup(id: "secondWindow") {
            VideoView()
                .zIndex(100)
            
        }
        .handlesExternalEvents(matching: Set(arrayLiteral: "secondWindow"))

        WindowGroup(id: "thirdWindow") {
            ImmersiveView()
                .zIndex(100)
        }
        .handlesExternalEvents(matching: Set(arrayLiteral: "thirdWindow"))
    }
}

Solution

  • You can't control where a new window appears in visionOS. The OS will place it front and center for the user. If you really want to control the location of all windows for your app, you'll have to use one single WindowGroup, with a transparent background, and place all of your content windows on that hosting superwindow. I don't think that's a good idea, because you're fighting the system framework and you're fighting user expectations on how the platform works. But if you really want to do that, that's a way forward.