2

I use in my Swift UI application for iOS NavigationViews (as .stack). Also I use nested NavigationView inside NavigationView and everything looked as I needed. But I decied to fefuse to use deprecated NavigationView and migrate to NavigationStack. But I got following issues.

I have 2 tabs (TabView). Each tab has NavigationStack with NavigationLinks. All content is wrapped inside NavigationStack because NavigationLink's destination views must cover full screen.

Issue 1: I can't show "toolbar" of List over List. Insede first tab I have some content which cover top part of Screen. Under this content I have List with EditMode. And when user clicks "Edit button" than "toolbar" with actions for List items must be right over this List (not on the top of screen). With NavigationViews this was solved by using additional NavigationView inside "main" NavigationView. But NavigatioinStack doesn't allow it. How I can display "toolbar" with action buttons right over List?

Issue 2: NavigationLink does not work with NavigationStack if List (which is inside) use "selection". On the first tab NavigatonLinks inside List must follow to view with details (which can folow to other actions like "edit" etc). But for "EditMode" I have to use List with "selection". And after adding "selection" NavigationLinks become "unclickable". "Selection" (with editMode=.active) works but clicks (with editMode=.inactive) on NavigationLinks don't work.

Thanks in advance for advices.

import SwiftUI

struct MyItemEntity: Hashable {let id: UUID, name: String}

struct TestNavigationStackView: View {
    
    @Environment(\.dismiss) var dismiss
    
    @State private var selectedTab: Int = 0
    
    @State var showSettingsOne: Bool = false
    @State var showSettingsTwo: Bool = false
    @State var showSettingsThree: Bool = false
    
    @State var editModeList: EditMode = .inactive //<- Declare the @State var for editMode
    
    var body: some View {
        if #available(iOS 16.0, *) {
            TabView(selection: $selectedTab) {
                NavigationStack {
                    VStack {
                        VStack {
                            Text("Home view")
                            Text("Some content on top part")
                            Spacer()
                            Button("Edit item", systemImage: "pencil") {
                                editModeList = editModeList == .inactive ? .active : .inactive
                            }
                            .foregroundColor(editModeList == .active ? .gray : .accentColor)
                        }
                        .frame(width: .infinity, height: 200)
                        ItemsListView(editModeList: $editModeList)
                    }
                }
                .tabItem() {
                    Image(systemName: "house.fill")
                }
                .tag(0)
                .navigationBarTitle("")
                .navigationBarHidden(true)
                
                NavigationStack {
                    List {
                        Button("Settings one") { showSettingsThree.toggle() }
                        Button("Settings two") { showSettingsThree.toggle() }
                        Button("Settings three") { showSettingsThree.toggle() }
                    }
                    .navigationDestination(isPresented: $showSettingsOne, destination: {
                        Text("Screen with settings one")
                    })
                    .navigationDestination(isPresented: $showSettingsTwo, destination: {
                        Text("Screen with settings two")
                    })
                    .navigationDestination(isPresented: $showSettingsThree, destination: {
                        Text("Screen with settings three")
                    })
                }
                .tabItem() {
                    Image(systemName: "gear")
                }
                .tag(1)
                .navigationBarTitle("")
                .navigationBarHidden(true)
            }
        } else {
            // Fallback on earlier versions
        }
    }
}

struct ItemsListView: View {
    
    @Binding var editModeList: EditMode
    @State private var selectedItems = Set<MyItemEntity>()
    
    var data = [
        MyItemEntity(id: UUID(), name: "John"),
        MyItemEntity(id: UUID(), name: "Bob"),
        MyItemEntity(id: UUID(), name: "Jack"),
        MyItemEntity(id: UUID(), name: "Garry"),
    ]
    
    var body: some View {
        if #available(iOS 16.0, *) {
            // !!! Click on NavigationLink does not work (details don't open) if use "selection"
            List(selection: self.$selectedItems) {
//            List {
                ForEach(data, id: \.id) { item in
                    NavigationLink(value: item) {
                        Text(item.name)
                    }
                    .tag(item)
                }
            }
            .navigationDestination(for: MyItemEntity.self) { item in
                ItemDetailsView(item: item)
            }
            // !!! I need this toolbar right here - over this List (not on the top of the screen)
            .toolbar(content: {
                ToolbarItem(placement: .topBarLeading) {
                    Button("", systemImage: selectedItems.count == 0 ? "circle" : "xmark.circle.fill") {
                        if selectedItems.count == 0 {
                            data.forEach { item in selectedItems.insert(item) }
                        } else {
                            selectedItems = Set<MyItemEntity>()
                        }
                    } // select/deselect all items button
                }
                ToolbarItem { Button("", systemImage: "trash") {} }
                ToolbarItem { Button("", systemImage: "tag") {} }
                ToolbarItem { Button("", systemImage: "ellipsis") {} }
            })
            .environment(\.editMode, $editModeList)
            .navigationBarHidden(editModeList == .active ? false : true)
        } else {
            // Fallback on earlier versions
        }
    }
}

struct ItemDetailsView: View {
    var item: MyItemEntity
    var body: some View {
        VStack {
            Text("Item name: \(item.name)")
            Button("Edit") {}
        }
    }
}

#Preview {
    TestNavigationStackView()
}

Toolbar position

3
  • 1
    selection only works with navigationSplitView you have to use a NavigationLink for the NavigationStack Commented Aug 13, 2024 at 13:56
  • @loremipsum Thanks for the answer. In my example selection works with NavigationStack with editMode=.active without issues. But NavigationLinks became not clickable in this case. Commented Aug 13, 2024 at 14:08
  • Issue 1 - I have solved by showing/hiding HStack with action buttons in place which I need instead of using regular toolbar. Commented Aug 14, 2024 at 5:31

1 Answer 1

0

2. Problem:

I would solve it by simply creating a NavigationLink that has the DetailView directly in the View Closure, so that you also transfer the item to your DetailView.

2. Solution:

Replace This Part:

List(selection: self.$selectedItems) {
  List {
    ForEach(data, id: \.id) { item in
        NavigationLink(value: item) {
            Text(item.name)
        }
        .tag(item)
    }
}
.navigationDestination(for: MyItemEntity.self) { item in
    ItemDetailsView(item: item)
}

By this Part:

List(selection: self.$selectedItems) {
    ForEach(data, id: \.id) { item in
       
        NavigationLink {
            ItemDetailsView(item: item)
        } label: {
            Text(item.name)
        }
        .tag(item)
       
    }
}

2. Explanation:

If you use NavigationLink(value: Hashable?) {...}, the view is automatically rendered as soon as the value 'item' is passed, but this can lead to problems, as in your example, that the NavigationDetail view does not open, so I usually use NavigationLink(destination: View(), label: View()). It is much simpler and gives you more options to customize it.

Hope this could help you.

Sign up to request clarification or add additional context in comments.

1 Comment

Have you noticed that replacement doesn't set the selection to nil when going back? Also if you set the selectedItem programatically the row highlights but it doesn't navigate. Not sure if these are bugs or they just expect us to use the NavigationStack(path:) and NavigationLink(value:) APIs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.