How to set size of WindowGroup in visionOS?

3.2k views Asked by At

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?

2

There are 2 answers

2
Andy Jazz On

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.

0
HeathHsia On

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)