FB17999226
As Sydney suggested, if you read the bounds in the make
closure you will get a bounds of 0. If you have state changes that trigger the update
closure to be called you can read the bounds there.
You can also use the onGeometryChange(for:of:action:) view modifier to get the new bounds when the view changes size. The following reads the view size from both within the view containing the RealityView and the view that is provided to the ViewAttachmentComponent:
struct ContentView: View {
@PhysicalMetric(from: .meters) var oneMeter = 1.0
@State var showMoreEntity = Entity()
@State var showMoreHeight: CGFloat = 0
var body: some View {
RealityView { content in
showMoreEntity.components.set(ViewAttachmentComponent(rootView: ShowMoreView()
.onGeometryChange(for: CGFloat.self) { proxy in
proxy.frame(in: .local).height
} action: { width in
showMoreHeight = width
}))
content.add(showMoreEntity)
}
.ornament(attachmentAnchor: .scene(.bottomFront)) {
Text("View Height:\n- \(showMoreHeight, specifier: "%.2f") meters \n- \(showMoreHeight / CGFloat(oneMeter), specifier: "%.2f") points")
.padding(20).glassBackgroundEffect()
}
}
}
struct ShowMoreView: View {
@PhysicalMetric(from: .meters) var oneMeter = 1.0
@State var showMore: Bool = false
@State var viewHeight: CGFloat = 0
var body: some View {
VStack {
Text("Height: \(viewHeight, specifier: "%.2f") points - \(viewHeight / oneMeter, specifier: "%.2f") meters")
Toggle(isOn: $showMore) { Text("Show more") }
if showMore { Text("Some more text") }
}
.padding(20).glassBackgroundEffect()
.onGeometryChange(for: CGFloat.self) { proxy in
proxy.frame(in: .local).height
} action: { width in
viewHeight = width
}
}
}
Let us know if you have any questions!
Michael