Window to Window container displacement

In Xcode 16 beta 6, we want to start the app with an Alert advising the user that they are about to enter an immersive space.

To achieve this, I use an empty VStack (lets name it View1) with an alert modifier. Then, in the alert’s OK button action, we have the statement openWindow(id: "ContentView”). View1 is in the first WindowGroup in the App file.

When pressing OK, the Alert and View1 dismiss themselves, then ContentView displays itself shifted vertically towards the top. ContentView is in a secondary WindowGroup. We should expect ContentView to display itself front and center to the user as every other window.

What is wrong my code? Or, is there a bug in visionOS?

Attached are images of my code, and a video illustrating the bad behavior.

Answered by VaiStardom in 803690022

Apple replied to my FB15058216 with a link to the pushWindow documentation that says : "Calling this action from a pushed window is not allowed.". So, we can't use pushWindow twice as to create deep view hierarchies.

Closing this issue as solved.

Hi @VaiStardom

I want to confirm you are aware that visionOS presents people with a similar warning by default, before they enter a fully immersive space.

That said, it's not currently possible to open a window directly over another window without creating a NavigationStack behind the scenes. In your case I recommend using a single plain window to conditionally show a view with glassBackgroundEffect modifier. Here's a snippet to demonstrate this:

import SwiftUI

@main
struct WindowPlacementApp: App {
    
    @State private var isPresented = true
    
    var body: some Scene {
        WindowGroup() {
            VStack {
                if isPresented == false {
                    VStack {
                        Text("Some text")
                    }
                    .frame(width: 1280, height: 720)
                    .glassBackgroundEffect()
                }
            }
            .alert("Some question?", isPresented: $isPresented) {
                VStack {
                    Button("Yes") {
                        //...
                    }
                }
            }
        }
        .windowStyle(.plain)
    }
}

Here are a couple of APIs that won't solve this issue, but are worth being aware of:

  • pushWindow - This action opens the requested window in place of the window the action is called from. The scene this action is called from will be backgrounded. The requested scene will be center-aligned with the backgrounded scene. The requested scene will have a default size that matches the size of the backgrounded scene. Closing the requested window will result in the backgrounded scene reappearing.

  • defaultWindowPlacement gives you control over the relative position of of windows. This is covered in Work with windows in SwiftUI.

Hello,

Thank you for your response.

I want to confirm you are aware that visionOS presents people with a similar warning by default, before they enter a fully immersive space.

Does this warning only appear in the physical device? Creating a simple test project with the below configuration does not display this alert in the simulator. How can I be certain the alert will display on the physical device, when it does not in the simulator upon pressing Show Immersive Space of the template project?

it's not currently possible to open a window directly over another window without creating a NavigationStack behind the scenes.

That is not what we are trying to achieve. Our designers would like to have the alert as the very first view, and then go directly into the ImmersiveSpace. We do not want to display any other view or volume as an initial scene.

When we chose our configuration for the project template, in Initial Scene it appears we cannot set ImmersiveSpace as an initial scene. I looks like we have no other choice but to display a view prior to displaying an immersive space.

a) Is this correct?

b) Or, is it indeed possible for an ImmersiveSpace to be the initial scene of our app?

Thank you for the other information you provided.

Hi @VaiStardom

RE: user warning

The information I gave you was out of date. As of visionOS 2.0 the prompt is not longer as pronounced. See this tread for more information.

RE: launch straight into immersive space

According to the best practice section of the HIG this is ok provided you launch into mixed immersion. You are correct, there is no option for this when creating a new project, but it is possible. To launch straight into an immersive space, change Preferred Default Scene Session Role to Immersive Space Application Session Role in your info.plist.

RE: That is not what we are trying to achieve

Thanks for clearing that up. I knew that wasn't your end goal, but thought it was part of the work around you were pursuing. EG: open an empty window with an alert then replace it with a new window.

Hello,

As of visionOS 2.0 the prompt is not longer as pronounced. See this tread for more information.

Ok, we removed the alert, thanks for informing.

change Preferred Default Scene Session Role to Immersive Space Application Session Role in your info.plist

Thanks you, this also worked.

FYI, pushWindow did work to resolve the vertical displacement. But, we encountered a new error with it.. It seems like there is a limit to the amount of windows we can push. I was hopeful with this pushWindow we could create view hierarchies, but we appear to be limited to two levels...

So, now the new issue is: The application failed to create new scene, scene limit exceeded..

Sorry for the piling of issues, ie: regional issue, it's resolution, and now encountering new issue..

Thanks you for your help.

Accepted Answer

Apple replied to my FB15058216 with a link to the pushWindow documentation that says : "Calling this action from a pushed window is not allowed.". So, we can't use pushWindow twice as to create deep view hierarchies.

Closing this issue as solved.

Window to Window container displacement
 
 
Q