How to set size of WindowGroup in visionOS?
Asked Answered
A

3

5

I want to set the dimensions of a WindowGroup to a specific size when running on visionOS. On iOS and maOS we have the .defaultSize modifier for that. When building for visionOS, Xcode offers a slightly different modifier (with an additional parameter called depth):

.defaultSize(width: 200.0, height: 200.0, depth: 200.0)

However this does not seem to have any effect — the size of my window in the simulator remains the same, even after a fresh app install.

How can I set a WindowGroup to a specific size on visionOS?

Amberjack answered 23/6, 2023 at 11:33 Comment(0)
B
5

Controlling SwiftUI's Volume dimensions

This method you specified, sets a default size for a volumetric window.

func defaultSize(width: CGFloat, 
                height: CGFloat,
                 depth: CGFloat) -> some Scene 

By default, each parameter (width, height, depth) is specified in points, which translates to 1 millimeter for volumetric scenes at standard system scale. The size of a volumetric scene is immutable after creation. If you want to follow the RealityKit scene-size-paradigm, use the initializer's fourth in parameter that allows you to set the size in meters.

For me, the following approach does the trick (read this post to see the content of a volume in my visionOS app).

import SwiftUI

@main struct ParticlesApp : App {
    var body: some Scene {
        WindowGroup {
            ContentView()                 // contains RealityKit scene
        }
        .windowStyle(.volumetric)
        .defaultSize(width: 2.0, height: 2.0, depth: 2.0, in: .meters)
    }
}


Controlling visionOS Window dimensions

visionOS SDK Beta 2 (Xcode 15 Beta 5)

In second beta of visionOS, there are some improvements on 2D window's resizability issue. You can control a size of window that is less or equal to 2m X 1m (but not greater than this size).

@main struct SomeApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()                 // contains 2D view
        }
        .defaultSize(width: 0.5, height: 0.5, depth: 0.0, in: .meters)
    }
}

enter image description here

visionOS SDK Beta 1

.windowResizability(..) modifier and all the other resizing methods are unavailable in visionOS Beta 1 (Xcode 15 beta 4 and lower).

enter image description here

You can find the identical question on the Developers Forums.

Baedeker answered 23/6, 2023 at 12:43 Comment(2)
Thank you Andy! 😊 Do you know if there’s a solution for a non-volumetric window in visionOS, too? (I assume that’s .windowStyle(.plain)?)Amberjack
Alas, I don't know how to fix this problem yet....Baedeker
L
1

First You can get all Scenes with UIApplication.shared.connectedScenes method

And find the current UIWindowScene with traversing all Scenes and UIWindowScene.session

Set the UIWindowScene size with UIWindowScene.requestGeometryUpdate method

// new size
let newSize = CGSize(width: 1000, height: 1000)
// create GeometryPreferences
let geometryPreferences = UIWindowScene.GeometryPreferences.Vision(resizingRestrictions: .uniform)
//  geometryPreferences set size
geometryPreferences.size = newSize
// set window size with requestGeometryUpdate method
currentWindowScene?.requestGeometryUpdate(geometryPreferences)
Lading answered 22/12, 2023 at 7:24 Comment(0)
R
0

I have testes on the real device, and if you do what apple suggests here: https://developer.apple.com/documentation/visionos/positioning-and-sizing-windows It would work and resize your app window size:

var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .windowStyle(.plain)
        .defaultSize(CGSize(width: 1500, height: 800))
}

It seems weird for me the fact that you are trying to give a depth to a window, can you elaborate what is that goal?

Reichel answered 22/4 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.