4

I recognized that my iOS app does have a double column navigation view style resp. split view for larger iPhones, like the iPhone 11 Pro Max, compared to a single column navigation.

I tried to get rid of this unwanted split view according to SwiftUI: unwanted split view on iPad by applying the .navigationViewStyle(StackNavigationViewStyle()) modifier to the NavigationView.

However, that introduces a new issue, where SwiftUI does not update the NavigationLink selection state after returning from a detail view. After coming back, the link is still shown to be active. After removing the .navigationViewStyle(StackNavigationViewStyle()) again, the selection state is updated correctly, so I think I am missing something.

I created a minimal reproducible example project. Please see the code below.

This image demonstrates the issue of a non-updating NavigationLink selection state after returning from a detail view when using the .navigationViewStyle(StackNavigationViewStyle()) modifier.

use of StackNavigationViewStyle does not update NavigationLink selection state after returning from detail view

Minimal reproducible example project:

import SwiftUI

@main
struct TestSwiftUIApp: App {
    var body: some Scene {
        WindowGroup {
            View1()
        }
    }
}
struct View1: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink(
                    destination: View2(),
                    label: {
                        Text("View2")
                    }
                ).isDetailLink(false)
                NavigationLink(
                    destination: View2(),
                    label: {
                        Text("View2")
                    }
                ).isDetailLink(false)
            }.listStyle(InsetGroupedListStyle())
            .navigationTitle("View1")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}
struct View2: View {
    @State var presentView3: Bool = false
    
    var body: some View {
        List {
            Text("Foo")
            NavigationLink("View3",
                           destination: View3(presentView3: $presentView3),
                           isActive: $presentView3
            ).isDetailLink(false)
            Text("Bar")
        }
        .listStyle(InsetGroupedListStyle())
        .navigationTitle("View 2")
    }
}
struct View3: View {
    @Binding var presentView3: Bool

    @State
    var isAddViewPresented: Bool = false

    var body: some View {
        List {
            Button(action: {presentView3 = false}, label: {
                Text("Dismiss")
            })
        }
        .listStyle(InsetGroupedListStyle())
        .navigationTitle("View3")
        .toolbar {
            ToolbarItem {
                Button(action: {isAddViewPresented.toggle()}, label: {
                    Label("Add", systemImage: "plus.circle.fill")
                })
            }
        }
        .sheet(isPresented: $isAddViewPresented, content: {
            Text("DestinationDummyView")
        })
    }
}
Bernhard
  • 703
  • 5
  • 25
  • If you do not need onDelete, use ScrollView & LazyVStack instead of List. – mahan Dec 22 '20 at 10:39
  • This looks like a bug, probably worth submitting to Apple. – pawello2222 Dec 22 '20 at 10:57
  • 1
    @pawello2222 Thanks for your opinion on this issue. I submitted a swift bug report via the Feedback Assistent. I will update this issue if I receive any response from Apple. – Bernhard Jan 12 '21 at 13:03

0 Answers0