I have a view that needs to know its parent's size when being initalized. Here's a simplified example of the code, where the Text
displays the size provided by the GeometryReader
.
struct ContentView: View {
var body: some View {
GeometryReader { proxy in
ZStack {
Rectangle()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.foregroundColor(.blue)
Text("Size: \(proxy.size.width) x \(proxy.size.height)")
}
}
}
}
When I run this code on iOS, the body
is updated once and the size is correct from the start. When I run this code on macOS, the body
is updated four times. The first three times the size is wrong and only the fourth time the size is correct.
In this example it does not matter all that much, but in my app the layout and animations depend on knowing the correct size of the parent view from the start.
Any ideas how I can make sure the Text
will only be initalized when the final size is available on macOS?