Search code examples
iosswiftxcodeswiftuiswiftui-navigationview

Body Render Issue with NavigationStack in XCode 14 Beta


In the following code, background color of the ContentView doesn't change after two seconds.

struct ContentView: View {
    @State private var bool: Bool = false
    
    var body: some View {
        NavigationStack(root: {
            Group(content: {
                if bool {
                    Color.red
                } else {
                    Color.blue
                }
            })
                .onAppear(perform: {
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
                        print(">>>")
                        bool = true
                    })
                })
        })
    }
}

However, when replacing NavigationStack(root: { with NavigationView(content: {, the problem disappears.

Is anyone facing the same problem, or am I doing something wrong? I am using XCode 14.0 beta 2 (14A5229c), and running it on iOS 16.0 (20A5303i). Issue happens on both, simulator and physical device.


Solution

  • Who knows... may be it is intentional may be it is a bug, submit feedback to Apple.

    Meanwhile here is a workaround, tested with Xcode 14 / iOS 16

        NavigationStack {
            if bool {
                Color.red
            } else {
                Color.blue
            }
        }
        .id(bool)             // << here !!
    

    *BTW, Group is not needed anymore in such cases.