I solved this eventually by adding another AnyView(...) to enclose all the view components inside the GeometryReader.
I tried all sorts of things including using @ViewBuilder for the variable and with a function to return the view but the problem remained. Adding AnyView(...) to enclose the elements in the GeometryReader was the only way I found to make it work.
@Published var testValue: Double = 0
var resultView: AnyView {
AnyView (VStack{
Text("Text From Object, Outside of GeometryReader")
Text("Value \(self.testValue.description)")
GeometryReader {
geom in
AnyView( -> AnyView added to enclose code within GeometryReader
VStack{
VStack {
Text("Text From Object Inside Geom Reader")
Text(self.testValue.description).frame(width: geom.size.width / 2, height: geom.size.height / 3)
}.background(Color.secondary)
}
) -> Close of AnyView added
} -> End of GeomReader
}
)
}
} // End of class
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: