Hello,
My goal is to have a NavigationStack whose root view is determined based on its height and width. To do so, I'm using ViewThatFits, which should choose the right view to display. It is working fine, but unexpectedly both views trigger onAppear, whereas only the appropriate one should. This causes the logic in both closures to be executed, which is not intended.
The code below demonstrates the problem:
struct NavigationStackContentView: View { var body: some View { NavigationStack { ViewThatFits(in: .vertical) { Color.yellow .onAppear { print("|-> on appear: yellow") } .onDisappear { print("|-> on disappear: yellow") } Color.red .frame(width: 1500, height: 1500) .onAppear { print("|-> on appear: red") } .onDisappear { print("|-> on disappear: red") } } } } }
this produces:
|-> on appear: red |-> on disappear: red |-> on appear: yellow
When ViewThatFits is not nested within NavigationStack, the problem does not occur — only the yellow view (in this sample) triggers onAppear, which is the expected behavior. I also checked the macOS version, and the problem does not occur at all, whether within NavigationStack or not.
This example is simple and demonstrates that the larger view is the second one. When I switch their places, the problem does not occur because it recognizes that the first view would not fit at this point. However, in my case I will have these views without knowing which one will not fit, so switching their order is not a viable solution if this works without NavigationStack.
Am I doing something wrong, or is this a bug?
// iOS: 18.3.1 Xcode: 16.2