Community!
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()
}