I am using the Xcode visionOS debugging tool to visualize the bounds of all the containers, but it shows my Entity is inside the Volume. Then why does it get clipped? Is there something wrong with the debugger, or am I missing something?
import SwiftUI
@main
struct RealityViewAttachmentApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.windowStyle(.volumetric)
.defaultSize(Size3D(width: 1, height: 1, depth: 1), in: .meters)
}
}
import SwiftUI
import RealityKit
import RealityKitContent
struct ContentView: View {
var body: some View {
RealityView { content, attachments in
if let earth = try? await Entity(named: "Scene", in: realityKitContentBundle) {
content.add(earth)
if let earthAttachment = attachments.entity(for: "earth_label") {
earthAttachment.position = [0, -0.15, 0]
earth.addChild(earthAttachment)
}
if let textAttachment = attachments.entity(for: "text_label") {
textAttachment.position = [-0.5, 0, 0]
earth.addChild(textAttachment)
}
}
} attachments: {
Attachment(id: "earth_label") {
Text("Earth")
}
Attachment(id: "text_label") {
VStack {
Text("This is just an example")
.font(.title)
.padding(.bottom, 20)
Text("This is just some random content")
.font(.caption)
}
.frame(minWidth: 100, maxWidth: 300, minHeight: 100, maxHeight: 300)
.glassBackgroundEffect()
}
}
}
}
Post
Replies
Boosts
Views
Activity
My app has a window and a volume. I am trying to display the volume on the right side of the window. I know .defaultWindowPlacement can achieve that, but I want more control over the exact position of my volume in relation to my window. I need the volume to move as I move the window so that it always stays in the same position relative to the window. I think I need a way to track the positions of both the window and the volume. If this can be achieved without immersive space, it would be great. If not, how do I do that in immersive space?
Current code:
import SwiftUI
@main
struct tiktokForSpacialModelingApp: App {
@State private var appModel: AppModel = AppModel()
var body: some Scene {
WindowGroup(id: appModel.launchWindowID) {
LaunchWindow()
.environment(appModel)
}
.windowResizability(.contentSize)
WindowGroup(id: appModel.mainViewWindowID) {
MainView()
.frame(minWidth: 500, maxWidth: 600, minHeight: 1200, maxHeight: 1440)
.environment(appModel)
}
.windowResizability(.contentSize)
WindowGroup(id: appModel.postVolumeID) {
let initialSize = Size3D(width: 900, height: 500, depth: 900)
PostVolume()
.frame(minWidth: initialSize.width, maxWidth: initialSize.width * 4, minHeight: initialSize.height, maxHeight: initialSize.height * 4)
.frame(minDepth: initialSize.depth, maxDepth: initialSize.depth * 4)
}
.windowStyle(.volumetric)
.windowResizability(.contentSize)
.defaultWindowPlacement { content, context in
// Get WindowProxy from context based on id
if let mainViewWindow = context.windows.first(where: { $0.id == appModel.mainViewWindowID }) {
return WindowPlacement(.trailing(mainViewWindow))
} else {
return WindowPlacement()
}
}
ImmersiveSpace(id: appModel.immersiveSpaceID) {
ImmersiveView()
.onAppear {
appModel.immersiveSpaceState = .open
}
.onDisappear {
appModel.immersiveSpaceState = .closed
}
}
.immersionStyle(selection: .constant(.progressive), in: .progressive)
}
}
On TikTok on Vision Pro, the home page has different minimum and maximum window heights and widths compared to the search page.
Now I am able to set minimum window size for different tab views but maximum size doesn't seem to work
Code:
// WindowSizeModel.swift
import Foundation
import SwiftUI
enum TabType {
case home
case search
case profile
}
@Observable
class WindowSizeModel {
var minWidth: CGFloat = 400
var maxWidth: CGFloat = 500
var minHeight: CGFloat = 400
var maxHeight: CGFloat = 500
func setWindowSize(for tab: TabType) {
switch tab {
case .home:
configureWindowSize(minWidth: 400, maxWidth: 500, minHeight: 400, maxHeight: 500)
case .search:
configureWindowSize(minWidth: 300, maxWidth: 800, minHeight: 300, maxHeight: 800)
case .profile:
configureWindowSize(minWidth: 800, maxWidth: 1000, minHeight: 800, maxHeight: 1000)
}
}
private func configureWindowSize(minWidth: CGFloat, maxWidth: CGFloat, minHeight: CGFloat, maxHeight: CGFloat) {
self.minWidth = minWidth
self.maxWidth = maxWidth
self.minHeight = minHeight
self.maxHeight = maxHeight
}
}
// tiktokForSpacialModelingApp.swift
import SwiftUI
@main
struct tiktokForSpacialModelingApp: App {
@State private var windowSizeModel: WindowSizeModel = WindowSizeModel()
var body: some Scene {
WindowGroup {
MainView()
.frame(
minWidth: windowSizeModel.minWidth, maxWidth: windowSizeModel.maxWidth,
minHeight: windowSizeModel.minHeight, maxHeight: windowSizeModel.maxHeight)
.environment(windowSizeModel)
}
.windowResizability(.contentSize)
}
}
// MainView.swift
import SwiftUI
import RealityKit
struct MainView: View {
@State private var selectedTab: TabType = TabType.home
@Environment(WindowSizeModel.self) var windowSizeModel;
var body: some View {
@Bindable var windowSizeModel = windowSizeModel
TabView(selection: $selectedTab) {
Tab("Home", systemImage: "play.house", value: TabType.home) {
HomeView()
}
Tab("Search", systemImage: "magnifyingglass", value: TabType.search) {
SearchView()
}
Tab("Profile", systemImage: "person.crop.circle", value: TabType.profile) {
ProfileView()
}
}
.onAppear {
windowSizeModel.setWindowSize(for: TabType.home)
}
.onChange(of: selectedTab) { oldTab, newTab in
if oldTab == newTab {
return
}
else if newTab == TabType.home {
windowSizeModel.setWindowSize(for: TabType.home)
}
else if newTab == TabType.search {
windowSizeModel.setWindowSize(for: TabType.search)
}
else if newTab == TabType.profile {
windowSizeModel.setWindowSize(for: TabType.profile)
}
}
}
}