I want to implement the functions in this video, how should I set the window
A few tips for you.
Placing windows
We can set the default position of a new window. We can position them relative to other windows or use utility window.
If you want to open a small window below the user, tilted up to face them, then you want .utility
.
Example
WindowGroup(id: "Rocks") {
Text("🪨🪨🪨")
.font(.system(size: 48))
}
.defaultSize(CGSize(width: 300, height: 100))
.defaultWindowPlacement { _, context in
return WindowPlacement(.utilityPanel)
}
If you want to place window relative to another window, then you can do something like this
// Main window
WindowGroup {
ContentView()
}
.defaultSize(width: 500, height: 500)
WindowGroup(id: "YellowFlower") {
Text("🌼")
.font(.system(size: 128))
}
.defaultSize(CGSize(width: 300, height: 200))
.defaultWindowPlacement { _, context in
if let mainWindow = context.windows.first {
return WindowPlacement(.leading(mainWindow))
}
return WindowPlacement(.none)
Unfortunately, as of visionOS 2, we can't move or position windows after they are already opened. The features above work once when we open a new window.
Sizing windows
You can control the default size like this
WindowGroup(id: "YellowFlower") {
YellowFlowerView()
}
.defaultSize(CGSize(width: 600, height: 600))
If you need more control, you can use .contentSize
to make the window adapt to the size of the view/content. You can even adjust the size of the view and the window will adapt.
WindowGroup(id: "YellowFlower") {
YellowFlowerView()
.frame(minWidth: 500, maxWidth: 700, minHeight: 500, maxHeight: 700)
}
.windowResizability(.contentSize)
.defaultSize(CGSize(width: 600, height: 600))
Hope this helps!